我目前正在尝试使用 Scrapey 在 python 中制作一个简单的爬虫。我想要它做的是读取链接列表并保存它们链接到的网站的 html。现在,我可以获得所有 URL,但无法弄清楚如何下载该页面。这是到目前为止我的蜘蛛的代码:
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from tutorial.items import BookItem
# Book scrappy spider
class DmozSpider(BaseSpider):
name = "book"
allowed_domains = ["learnpythonthehardway.org"]
start_urls = [
"http://www.learnpythonthehardway.org/book/",
]
def parse(self, response):
filename = response.url.split("/")[-2]
file = open(filename,'wb')
file.write(response.body)
file.close()
hxs = HtmlXPathSelector(response)
sites = hxs.select('//ul/li')
items = []
for site in sites:
item = BookItem()
item['title'] = site.select('a/text()').extract()
item['link'] = site.select('a/@href').extract()
items.append(item)
return items
最佳答案
在您的 parse
方法中,返回返回项目列表中的 Request
对象以触发下载:
for site in sites:
...
items.append(item)
items.append(Request(item['link']), callback=self.parse)
这将导致爬虫为每个链接生成一个 BookItem
,同时还会递归并下载每本书的页面。当然,如果您想以不同的方式解析子页面,您可以指定不同的回调(例如 self.parsebook
)。
关于python - 使用scrapy创建一个简单的python爬虫,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12153671/