Python 网络爬虫:深入解析 Scrapy
warning:
这篇文章距离上次修改已过188天,其中的内容可能已经有所变动。
import scrapy
class MySpider(scrapy.Spider):
name = 'example.com'
allowed_domains = ['example.com']
start_urls = ['http://www.example.com']
def parse(self, response):
# 提取所有的新闻条目并依次解析
for href in response.css('ul.links a::attr(href)').getall():
url = response.urljoin(href)
yield scrapy.Request(url, callback=self.parse_detail)
# 提取下一页链接并请求
next_page_url = response.css('a.next-page::attr(href)').get()
if next_page_url is not None:
next_page_url = response.urljoin(next_page_url)
yield scrapy.Request(next_page_url, callback=self.parse)
def parse_detail(self, response):
# 提取新闻详情数据
title = response.css('h1::text').get()
content = response.css('div.content::text').get()
yield {
'title': title,
'content': content,
}
这个简单的爬虫示例展示了如何使用Scrapy框架来创建一个简单的网络爬虫。它定义了一个名为example.com
的爬虫,它将从http://www.example.com
开始,并提取该页面上所有新闻条目的链接,然后对每个新闻条目进行详细信息的抓取,并最终生成包含标题和内容的数据。
评论已关闭