【问题标题】:Why can I only scrape first 4 pages of results on eBay?为什么我只能在 eBay 上抓取结果的前 4 页?
【发布时间】:2021-01-07 00:33:05
【问题描述】:

我有一个简单的脚本来分析 eBay 上的销售数据(棒球交易卡)。前 4 页似乎工作正常,但在第 5 页上,它根本不再加载所需的 html 内容,我无法弄清楚为什么会发生这种情况:

#Import statements
import requests
import time
from bs4 import BeautifulSoup as soup
from tqdm import tqdm
#FOR DEBUG
Page_1="https://www.ebay.com/sch/213/i.html?_from=R40&LH_Sold=1&_sop=16&_pgn=1"

#Request URL working example
source=requests.get(Page_1)
time.sleep(5)
eBay_full = soup(source.text, "lxml")
Complete_container=eBay_full.find("ul",{"class":"b-list__items_nofooter"})
Single_item=Complete_container.find_all("div",{"class":"s-item__wrapper clearfix"})
items=[]
#For all items on page perform desired operation
for i in tqdm(Single_item):
    items.append(i.find("a", {"class": "s-item__link"})["href"].split('?')[0].split('/')[-1])
    #Works fine for Links_to_check[0] upto Links_to_check[3]

但是,当我尝试抓取第五页或更多页时,会发生以下情况:

Page_5="https://www.ebay.com/sch/213/i.html?_from=R40&LH_Sold=1&_sop=16&_pgn=5"

source=requests.get(Page_5)
time.sleep(5)
eBay_full = soup(source.text, "lxml")
Complete_container=eBay_full.find("ul",{"class":"b-list__items_nofooter"})
Single_item=Complete_container.find_all("div",{"class":"s-item__wrapper clearfix"})
items=[]
#For all items on page perform desired operation
for i in tqdm(Single_item):
    items.append(i.find("a", {"class": "s-item__link"})["href"].split('?')[0].split('/')[-1])

----> 5 Single_item=Complete_container.find_all("div",{"class":"s-item__wrapper clearfix"})
      6 items=[]
      7 #For all items on page perform desired operation

AttributeError: 'NoneType' object has no attribute 'find_all'

这似乎是后面页面的 eBay_full 汤中缺少 ul 类 b-list__items_nofooter 的逻辑结果。然而问题是为什么缺少这些信息?滚动浏览汤,所有感兴趣的项目似乎都不存在。正如预期的那样,该信息在网页本身上是存在的。谁能指导我?

【问题讨论】:

  • 您可以考虑在使用 requests 时使用 header 参数。网站试图保护自己免受机器人的侵害......
  • 这似乎解决了问题。谢谢

标签: python html web-scraping beautifulsoup python-requests


【解决方案1】:

按照@Sebastien D 的说法,问题已经解决了

在 headers 变量中只放置这些浏览器之一,以及当前的稳定版本号(例如 Chrome/53.0.2785.143,最新发现的here

headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'}

source= requests.get(Page_5, headers=headers, timeout=2)

【讨论】:

  • 当我运行该请求时,我收到一个关于 ReadTimeout: HTTPSConnectionPool(host='', port=443): Read timed out. (read timeout=2) 的错误,当我在没有超时的情况下运行它时它也不起作用
猜你喜欢
  • 2020-06-10
  • 2023-01-16
  • 2022-01-27
  • 2021-08-01
  • 2013-11-30
  • 2020-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多