用selenium写了个京东评论爬虫,为什呢老跳入中断,不中断就报错下一页标签找不到,或者说下一页标签不可点击,求解答!!!


```python
def get_next_page():
    try:
    # 下一页url
        time.sleep(3)
        driver.find_element_by_xpath('//div[@class="com-table-footer"]//a[@class="ui-pager-next"]').click()
    except Exception:
        print(40)
        ipdb.set_trace()


def get_datas():
    # 获取每页的评论
    commentlist = driver.find_elements_by_xpath('//div[@id="comment-0"]//div/div[2]/p')
    # 爬取文字评论
    comment = [c.text for c in commentlist]
    data = {"title": title, "comment": comment}
    collection.insert_one(data)
    time.sleep(5)


driver = webdriver.Chrome()
keyword = input("输入商品名称")
url = 'https://search.jd.com/Search?keyword=' + quote(keyword) + '&enc=utf-8'
driver.get(url)
time.sleep(3)
goods = driver.find_elements_by_xpath('//div[@id="J_goodsList"]/ul/li/div/div[4]/a')
links = [good.get_attribute('href') for good in goods]
title = driver.find_element_by_xpath('//div[@id="J_goodsList"]/ul/li[1]/div/div[4]/a').get_attribute('title')
for link in links:
    driver.get(link)
    time.sleep(3)
    #点击商品评论
    driver.find_element_by_xpath('//div[@id="detail"]/div[1]/ul/li[5]').click()
    time.sleep(10)
    while True:
        next_page = driver.find_element_by_xpath('//div[@class="com-table-footer"]//a[@class="ui-pager-next"]')
        print(1)
        if next_page:
            number = 400
            # 循环拖动鼠标滚轮,使当前页的所有商品信息加载完成
            for line in range(20):
                js = 'window.scrollTo(0,%s)' % number
                number += 500
                driver.execute_script(js)
                time.sleep(2)
            get_datas()
            time.sleep(10)
            get_next_page()
            time.sleep(10)
        else:
            break
driver.close()

![在这里插入图片描述](https://img-blog.csdnimg.cn/202002121726434![在这里插入图片描述](https://img-blog.csdnimg.cn/20200212172656675.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NDAwMTEy,size_16,color_FFFFFF,t_70)39.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NDAwMTEy,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200212172711174.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NDAwMTEy,size_16,color_FFFFFF,t_70)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值