【问题标题】:How to increase the request page time in python 3 while scraping web pages?如何在抓取网页时增加python 3中的请求页面时间?
【发布时间】: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


【解决方案1】:

错误说明了一切:

C:\Users\prate\Desktop\Crawler\Git_Crawler\New>python scrape.py Traceback (most recent call last): File "scrape.py", line 37, in WebDriverWait(browser, timeout=10).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "_2xg6Ul"))) File "C:\Users\prate\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:

如果您查看expected_conditions 子句presence_of_all_elements_located(locator)API Docs,它被定义为:

An expectation for checking that there is at least one element present on a web page. locator is used to find the element returns the list of WebElements once they are located

现在,如果您浏览到预期的网页:

https://www.flipkart.com/honor-8-pro-midnight-black-128-gb/product-reviews/itmeymafrghbjcpf?page=1&pid=MOBEWXHMVYBBMZGJ

您会发现 网页 没有产品或评论,并且您已将 Locator Strategy 改编为 (By.CLASS_NAME, "_2xg6Ul") 不识别网页上的任何元素。

因此,即使同步时间过去,webelements 也不会添加到 list 中,并且会引发 selenium.common.exceptions.TimeoutException

正如您所提到的代码在像 Amazon 这样的平台上运行良好值得一提的是,网站 https://www.flipkart.com 是基于 ReactJS 的,可能与 不同网站网站

【讨论】:

  • 感谢 Debanjan 回答这个问题。我查看了这个,发现实际上在 Flipkart 第 15 页上没有评论,但还有一件事,他们说他们有 155 页的评论,但大多数页面没有显示任何评论,如果我去第 20 页,一次它不会在页面上显示任何内容,但如果我在尝试 3-4 个其他页面后再次进入该页面,它将在那里显示评论。没拿到这个东西,怎么做flipkart的完美爬虫?
  • 我想知道我应该写什么作为我在新问题中面临的问题的标题?有什么技术术语吗?这里没有模式。如果我继续刷新评论页面 15 或 20 或 51,10 次中的 2 次我可能会获得数据(评论)或者我可能会在 5 次刷新后获得数据,等等。
猜你喜欢
  • 2022-01-18
  • 2018-03-22
  • 2016-02-21
  • 1970-01-01
  • 2020-04-20
  • 1970-01-01
  • 1970-01-01
  • 2022-11-19
  • 1970-01-01
相关资源
最近更新 更多