gpt4 book ai didi

python - BeautifulSoup 看似随机地从页面中提取 23、42 或 87 个结果,尽管页面有 100 个结果

转载 作者:行者123 更新时间:2023-11-30 23:13:01 24 4
gpt4 key购买 nike

编辑/更新:我开始使用 BeautifulSoup 作为下面提到的 alecxe,但现在我收到了看似随机的结果范围。有时它返回 23,大多数时候返回 42,有时返回 87。如果我重新转义同一页面,我不会得到相同的结果。 95% 的时间它会检索 42 个项目...有人知道发生了什么事吗? Here's a poorly cropped version of the varying results(完整尺寸链接:http://i.imgur.com/YeeupLh.png)

我使用了与 Alecxe 在这里看到的类似的代码,但我相信两者都有相同的问题

def parse(self, response):
data = json.loads(response.body)['results_html']
soup = BeautifulSoup(data, "lxml")

prices = [float(price.strip(r"\r\n\t").replace('$','').split(" ")[0])
for price in soup.find_all(text=re.compile(r"USD"))]

我的部分代码可以在这里看到:http://pastebin.com/y7hypCmv

(上一页)无论出于何种原因,我的抓取工具坚持抓取 19 页结果,而不是可用的 100 页结果。这是我的蜘蛛:

from scrapy import Request, Spider
from scrapy.selector import Selector
from idem.items import IdemItem


URL = 'http://steamcommunity.com/market/search/render/?query=&start={page}&count=100' # Note, this is pre-formatted HTML

class MySpider(Spider):
handle_httpstatus = 200
name = "postings"

def start_requests(self):
index = 0
while True:
yield Request(URL.format(page=index))
index +=100
if index >= 200: break
def parse(self,response):
sel = Selector(response)
items = []
item = IdemItem()
item["price"] = sel.xpath("//text()[contains(.,'$')]").extract()
item["supply"] = sel.xpath("//span[@class[contains(.,'market_listing_num')]]/text()").extract()
item["_id"] = sel.xpath("//span[@class[contains(.,'market_listing_item_name')]]/text()[1]").extract()

for price, supply, _id in zip(item["price"], item["supply"], item["_id"]):
item = IdemItem()
item["price"] = float(price.strip(r"\r\n\t").replace('$',''))
item["supply"] = int(supply.strip(r"\r\n\t").replace(',',''))
item["_id"] = _id.strip(r"\r\n\t").replace(r'u2605','\u2605').decode('unicode-escape')
items.append(item)
return items

如果我更改 count=19 和索引 +=19,我可以提取所有数据,但我宁愿同时抓取所有 100 个列表!

这是抓取后的 shell 结果:

  'downloader/request_count': 2,
'downloader/request_method_count/GET': 2,
'downloader/response_bytes': 31456,
'downloader/response_count': 2,
'downloader/response_status_count/200': 2,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2015, 4, 10, 16, 13, 46, 409000),
'item_scraped_count': 38, #-----(19 results x 2 pages)-------#
'log_count/DEBUG': 80,
'log_count/INFO': 7,
'response_received_count': 2,
'scheduler/dequeued': 2,
'scheduler/dequeued/memory': 2,
'scheduler/enqueued': 2,
'scheduler/enqueued/memory': 2,
'start_time': datetime.datetime(2015, 4, 10, 16, 13, 44, 808000)}

真的任何建议都会有帮助!

最佳答案

这是对我有用的东西(需要安装BeautifulSoup):

def parse(self, response):
data = json.loads(response.body)['results_html']
soup = BeautifulSoup(data, "lxml")

prices = [float(price.strip(r"\r\n\t").replace('$','').split(" ")[0])
for price in soup.find_all(text=re.compile(r"USD"))]

价格将是这样的列表:

[0.08, 0.08, 0.04, 0.08, 0.05, 0.11, 0.08, 0.03, 0.06, 0.07, 0.06, 0.06, 0.11, 0.07, 0.08, 0.08, 0.07, 0.07, 0.08, 0.08, 0.12, 0.08, 0.07, 0.11, .
...
0.04, 0.11, 0.04, 0.04, 0.04, 0.06, 0.04, 0.09, 0.06, 0.12, 0.04, 0.06, 0.07, 0.04, 0.05, 0.04]

仅供引用,我尝试了不同的定位技术,但尚未使其仅适用于 Scrapy

关于python - BeautifulSoup 看似随机地从页面中提取 23、42 或 87 个结果,尽管页面有 100 个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29566202/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com