```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)