用scrapy框架写一个爬虫
时间: 2024-05-29 07:03:33 浏览: 132
好的,我可以提供一个简单的爬虫示例,用 Scrapy 爬取笔者个人博客的文章列表和详情。首先,需要安装 Scrapy:
```
pip install scrapy
```
然后,在终端中输入以下命令,创建一个新的 Scrapy 项目:
```
scrapy startproject myblog
```
接下来,切换到项目根目录,创建一个新的爬虫:
```
cd myblog
scrapy genspider blog_spider myblog.com
```
这样就创建了一个名为 `blog_spider` 的爬虫,接下来我们需要在 `blog_spider.py` 文件中编写我们的爬虫逻辑。
首先,我们需要在 `start_requests` 方法中发起初始请求,获取文章列表页的 HTML 内容:
```python
import scrapy
class BlogSpider(scrapy.Spider):
name = 'blog_spider'
allowed_domains = ['myblog.com']
start_urls = ['http://myblog.com/articles']
def start_requests(self):
for url in self.start_urls:
yield scrapy.Request(url=url, callback=self.parse_list)
```
接下来,我们需要在 `parse_list` 方法中解析文章列表页的 HTML,获取每篇文章的 URL,并发起请求获取文章详情:
```python
def parse_list(self, response):
for article in response.css('.article'):
article_url = article.css('a::attr(href)').get()
yield scrapy.Request(url=article_url, callback=self.parse_article)
next_page_url = response.css('a.next-page::attr(href)').get()
if next_page_url:
yield scrapy.Request(url=next_page_url, callback=self.parse_list)
```
在上面的代码中,我们使用了 CSS 选择器来获取文章列表页中所有文章链接的 URL,并且判断是否有下一页,如果有下一页就继续发起请求获取下一页的 HTML 内容。
最后,我们需要在 `parse_article` 方法中解析文章详情的 HTML,提取文章的标题和内容:
```python
def parse_article(self, response):
title = response.css('h1::text').get()
content = response.css('.article-content').get()
yield {
'title': title,
'content': content,
}
```
这样就完成了一个简单的爬虫,可以通过以下命令运行:
```
scrapy crawl blog_spider
```
完整的代码如下:
```python
import scrapy
class BlogSpider(scrapy.Spider):
name = 'blog_spider'
allowed_domains = ['myblog.com']
start_urls = ['http://myblog.com/articles']
def start_requests(self):
for url in self.start_urls:
yield scrapy.Request(url=url, callback=self.parse_list)
def parse_list(self, response):
for article in response.css('.article'):
article_url = article.css('a::attr(href)').get()
yield scrapy.Request(url=article_url, callback=self.parse_article)
next_page_url = response.css('a.next-page::attr(href)').get()
if next_page_url:
yield scrapy.Request(url=next_page_url, callback=self.parse_list)
def parse_article(self, response):
title = response.css('h1::text').get()
content = response.css('.article-content').get()
yield {
'title': title,
'content': content,
}
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)