【发布时间】:2018-02-19 20:43:53
【问题描述】:
我已经开始从电子商务平台上抓取评论并进行情感分析并在我的博客上与人们分享,以使人们的生活更轻松,并在一篇文章中了解有关产品的所有信息。 我正在使用像 selenium 和 bs4 这样的 python 包。这是我的代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
from contextlib import closing
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver import Firefox
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time
import requests
import re
from bs4 import BeautifulSoup
def remove_non_ascii_1(text):
return ''.join([i if ord(i) < 128 else ' ' for i in text])
with closing(Firefox()) as browser:
site = "https://www.flipkart.com/honor-8-pro-midnight-black-128-gb/product-reviews/itmeymafrghbjcpf?page=1&pid=MOBEWXHMVYBBMZGJ"
browser.get(site)
file = open("review.txt", "w")
for count in range(1, 100):
nav_btns = browser.find_elements_by_class_name('_33m_Yg')
button = ""
for btn in nav_btns:
number = int(btn.text)
if(number==count):
button = btn
break
button.send_keys(Keys.RETURN)
WebDriverWait(browser, timeout=10).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "_2xg6Ul")))
read_more_btns = browser.find_elements_by_class_name('_1EPkIx')
for rm in read_more_btns:
browser.execute_script("return arguments[0].scrollIntoView();", rm)
browser.execute_script("window.scrollBy(0, -150);")
rm.click()
page_source = browser.page_source
soup = BeautifulSoup(page_source, "lxml")
ans = soup.find_all("div", class_="_3DCdKt")
for tag in ans:
title = str(tag.find("p", class_="_2xg6Ul").string).replace(u"\u2018", "'").replace(u"\u2019", "'")
title = remove_non_ascii_1(title)
title.encode('ascii','ignore')
content = tag.find("div", class_="qwjRop").div.prettify().replace(u"\u2018", "'").replace(u"\u2019", "'")
content = remove_non_ascii_1(content)
content.encode('ascii','ignore')
content = content[15:-7]
votes = tag.find_all("span", class_="_1_BQL8")
upvotes = int(votes[0].string)
downvotes = int(votes[1].string)
file.write("Review Title : %s\n\n" % title )
file.write("Upvotes : " + str(upvotes) + "\n\nDownvotes : " + str(downvotes) + "\n\n")
file.write("Review Content :\n%s\n\n\n\n" % content )
file.close()
代码在亚马逊等平台上运行良好,但在 Flipkart 上,爬取 14 个页面后,我收到一条错误消息“Someting is Wrong!!!”爬行停止。 在命令行中我收到此错误:
C:\Users\prate\Desktop\Crawler\Git_Crawler\New>python scrape.py 回溯(最近一次通话最后): 文件“scrape.py”,第 37 行,在 WebDriverWait(浏览器, timeout=10).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "_2xg6Ul"))) 文件“C:\Users\prate\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\support\wait.py”,第 80 行,直到 引发 TimeoutException(消息、屏幕、堆栈跟踪) selenium.common.exceptions.TimeoutException:消息:
也没有打印任何消息。我想如果我增加平台上的请求时间间隔,它可能会让我爬行。 我该怎么办?
【问题讨论】:
-
如果您确定该元素应该出现在页面上并且您只需要更多时间来等待它出现,然后将
WebDriverWait(browser, timeout=10).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "_2xg6Ul")))中的超时10替换为例如20跨度> -
感谢安德森,但它不起作用。刮了 14 页后出现此错误,请查看链接:ibb.co/egBpGS
标签: python-3.x selenium web-crawler python-requests