gpt4 book ai didi

python - 如何使用scrapy从主脚本中获取抓取的项目?

转载 作者:太空狗 更新时间:2023-10-30 01:27:36 26 4
gpt4 key购买 nike

我希望在主脚本中获得一个已抓取项目的列表,而不是使用 scrapy shell。

我知道在我定义的FooSpider类中有一个parse方法,这个方法返回一个Item列表。 Scrapy 框架调用这个方法。但是,我怎样才能自己得到这个返回的列表呢?

我找到了很多关于那个的帖子,但我不明白他们在说什么。

作为上下文,我将官方示例代码放在这里

import scrapy

from tutorial.items import DmozItem

class DmozSpider(scrapy.Spider):
name = "dmoz"
allowed_domains = ["dmoz.org"]
start_urls = [
"http://www.dmoz.org/Computers/Programming/Languages/Python/",
]

def parse(self, response):
for href in response.css("ul.directory.dir-col > li > a::attr('href')"):
url = response.urljoin(href.extract())
yield scrapy.Request(url, callback=self.parse_dir_contents)

def parse_dir_contents(self, response):
result = []
for sel in response.xpath('//ul/li'):
item = DmozItem()
item['title'] = sel.xpath('a/text()').extract()
item['link'] = sel.xpath('a/@href').extract()
item['desc'] = sel.xpath('text()').extract()
result.append(item)

return result

如何从 main.pyrun.py 等主要 Python 脚本返回 result

if __name__ == "__main__":
...
result = xxxx()
for item in result:
print item

谁能提供一个代码片段,我可以在其中从某处获取返回的 list

非常感谢!

最佳答案

这是一个示例,您可以如何使用管道收集列表中的所有项目:

#!/usr/bin/python3

# Scrapy API imports
import scrapy
from scrapy.crawler import CrawlerProcess

# your spider
from FollowAllSpider import FollowAllSpider

# list to collect all items
items = []

# pipeline to fill the items list
class ItemCollectorPipeline(object):
def __init__(self):
self.ids_seen = set()

def process_item(self, item, spider):
items.append(item)

# create a crawler process with the specified settings
process = CrawlerProcess({
'USER_AGENT': 'scrapy',
'LOG_LEVEL': 'INFO',
'ITEM_PIPELINES': { '__main__.ItemCollectorPipeline': 100 }
})

# start the spider
process.crawl(FollowAllSpider)
process.start()

# print the items
for item in items:
print("url: " + item['url'])

你可以从here得到FollowAllSpider ,或使用您自己的蜘蛛。将它用于我的网页时的示例输出:

$ ./crawler.py 
2018-09-16 22:28:09 [scrapy.utils.log] INFO: Scrapy 1.5.1 started (bot: scrapybot)
2018-09-16 22:28:09 [scrapy.utils.log] INFO: Versions: lxml 3.7.1.0, libxml2 2.9.4, cssselect 1.0.3, parsel 1.5.0, w3lib 1.19.0, Twisted 18.7.0, Python 3.5.3 (default, Jan 19 2017, 14:11:04) - [GCC 6.3.0 20170118], pyOpenSSL 16.2.0 (OpenSSL 1.1.0f 25 May 2017), cryptography 1.7.1, Platform Linux-4.9.0-6-amd64-x86_64-with-debian-9.5
2018-09-16 22:28:09 [scrapy.crawler] INFO: Overridden settings: {'USER_AGENT': 'scrapy', 'LOG_LEVEL': 'INFO'}
[...]
2018-09-16 22:28:15 [scrapy.core.engine] INFO: Spider closed (finished)
url: http://www.frank-buss.de/
url: http://www.frank-buss.de/impressum.html
url: http://www.frank-buss.de/spline.html
url: http://www.frank-buss.de/schnecke/index.html
url: http://www.frank-buss.de/solitaire/index.html
url: http://www.frank-buss.de/forth/index.html
url: http://www.frank-buss.de/pi.tex
[...]

关于python - 如何使用scrapy从主脚本中获取抓取的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38187891/

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