gpt4 book ai didi

python - 使用 scrapy 在多页上递归获取链接

转载 作者:太空宇宙 更新时间:2023-11-03 18:19:02 30 4
gpt4 key购买 nike

我正在使用我在网上找到的以下代码来递归地抓取多个页面上的链接。它应该递归地返回所有页面上我需要的所有链接。但我最终最多只获得 100 个链接。任何建议都会有帮助。

class MySpider(CrawlSpider):
name = "craigs"
allowed_domains = ["craigslist.org"]
start_urls = ["http://seattle.craigslist.org/search/jjj?is_parttime=1"]

rules = (Rule (SgmlLinkExtractor(allow=("index\d00\.html", ),restrict_xpaths=('//a[@class="button next"]',))
, callback="parse_items", follow= True),
)

def parse_items(self, response):
hxs = HtmlXPathSelector(response)
titles = hxs.select('//span[@class="pl"]')
items = []
for titles in titles:
item = CraigslistSampleItem()
item ["title"] = titles.select("a/text()").extract()
item ["link"] = titles.select("a/@href").extract()
items.append(item)
return(items)

最佳答案

只需删除 allow=("index\d00\.html", ) 即可让它解析 next 链接:

rules = (Rule(SgmlLinkExtractor(restrict_xpaths=('//a[@class="button next"]',)),           
callback="parse_items", follow= True),)

关于python - 使用 scrapy 在多页上递归获取链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24493773/

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