会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132358个问题
Python 全系列/第十五阶段:Python 爬虫开发/scrapy框架使用 241楼
Python 全系列/第十五阶段:Python 爬虫开发/scrapy框架使用 242楼

老师就是网页没有爬取完全,程序就结束了,然后我加了隐式等待和调用js,但是网页还是没有爬取完全,是不是我的逻辑方面有错误,爬取时,还会有那个提示弹窗,必须手动点掉才开始爬取,怎么在代码里就给他跳过

from selenium.webdriver.chrome.service import Service

from selenium.webdriver import Chrome

from lxml import etree

from selenium.webdriver.common.by import By

from time import sleep


def spider_huya():

    #创建驱动

    service=Service('./chromedriver.exe')

    #创建浏览器

    driver=Chrome(service=service)

    #访问网页

    driver.get('https://www.huya.com/g/lol')

    count=1

    while True:

        print('获取了第{%d}页' % count)

        count +=1

        #拖动滚动条到底部

        js='document.documentElement.scrollTop=1000'

        driver.execute_script(js)

         #设置隐式等待

        driver.implicitly_wait(20)

        #创建etree对象

        e=etree.HTML(driver.page_source)

        #提取数据

        names=e.xpath('//i[@class="nick"]/text()')

        persons=e.xpath('//i[@class="js-num"]/text()')

        sleep(3)

       

        #打印数据

        #for n,p in zip(names,persons):

             #print(f'主播名:{n} 人气:{p}')

        #获取选项卡,点击

        if driver.page_source.find('laypage_next') ==-1:

            break

        next_btn=driver.find_element(By.XPATH,'//a[@class="laypage_next"]')

        next_btn.click()

        sleep(3)

    driver.quit()

 

if  __name__=='__main__':

    spider_huya()

594d0d95d70c3aaa787599017a3c0d3.png

Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础 244楼
Python 全系列/第十五阶段:Python 爬虫开发/scrapy框架使用 245楼
Python 全系列/第十五阶段:Python 爬虫开发/scrapy框架使用 247楼
Python 全系列/第十五阶段:Python 爬虫开发/移动端爬虫 248楼
Python 全系列/第十五阶段:Python 爬虫开发/移动端爬虫 249楼

#pip install Appium-Python-Client==2.6.1

#selenium                  4.11.2

from appium import webdriver

from selenium.webdriver.common.by import By


from time import sleep


#设置设备信息

desired = {

    "platformName": "Android",

    "platformVersion": "5.1.1",

    "deviceName": "VOG-AL10",

    "appPackage": "com.android.browser",

    "appActivity": "com.android.browser.BrowserActivity"

}


#设置appium的服务

server = 'http://localhost:4723/wd/hub'


# 新建一个driver

driver = webdriver.Remote(server, options=desired)


#蒙版元素,需要点击一下

value = '/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView'

driver.find_element(By.XPATH,value).click()

sleep(1)

#定位输入框

input_text = '/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[2]/android.view.View[3]/android.view.View/android.widget.EditText'

input = driver.find_element(By.XPATH,input_text)

#输入内容,清空数据

input.clear()

input.send_keys('音乐')

sleep(1)

#定位按钮

button_text = '/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[2]/android.view.View[3]/android.view.View/android.widget.Button'

button = driver.find_element(By.XPATH,button_text)

#点击按钮

button.click()

sleep(1)


#com.android.browser/.BrowserActivity






启动报错:

Traceback (most recent call last):

  File "d:\python\spiderProject\移动端爬虫\appium的实战.py", line 21, in <module>

    driver = webdriver.Remote(server, options=desired)

  File "D:\virtualenv\spider_env\lib\site-packages\appium\webdriver\webdriver.py", line 230, in __init__

    super().__init__(

TypeError: WebDriver.__init__() got an unexpected keyword argument 'desired_capabilities'


Python 全系列/第十五阶段:Python 爬虫开发/移动端爬虫 250楼
Python 全系列/第十五阶段:Python 爬虫开发/移动端爬虫 253楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础 254楼
Python 全系列/第十五阶段:Python 爬虫开发/移动端爬虫 255楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637