爬取千里马招标网站数据
时间: 2023-10-05 09:14:30 浏览: 138
爬取千里马招标网站数据可以使用Python中的爬虫框架Scrapy。
首先,使用Scrapy创建一个新的爬虫项目:
```
scrapy startproject qianlima
```
然后,在项目目录下创建一个新的Spider:
```
cd qianlima
scrapy genspider qianlima_spider www.qianlima.com
```
接下来,在Spider中编写爬取代码。首先需要在Spider中定义要爬取的URL:
```python
start_urls = ['http://www.qianlima.com/zb/search.html?keywords=医院']
```
然后编写解析函数,从页面中提取出需要的数据:
```python
def parse(self, response):
# 解析页面
for item in response.xpath('//div[@class="list_item"]'):
# 提取数据
title = item.xpath('div[@class="list_item_title"]/a/text()').extract_first()
link = item.xpath('div[@class="list_item_title"]/a/@href').extract_first()
pub_date = item.xpath('div[@class="list_item_time"]/text()').extract_first()
# 将数据存储到Item中
yield {
'title': title,
'link': link,
'pub_date': pub_date,
}
```
最后,在settings.py中启用自动限速:
```python
AUTOTHROTTLE_ENABLED = True
```
这样就完成了爬取千里马招标网站数据的操作。完整的代码如下:
```python
import scrapy
class QianlimaSpider(scrapy.Spider):
name = 'qianlima_spider'
allowed_domains = ['www.qianlima.com']
start_urls = ['http://www.qianlima.com/zb/search.html?keywords=医院']
def parse(self, response):
# 解析页面
for item in response.xpath('//div[@class="list_item"]'):
# 提取数据
title = item.xpath('div[@class="list_item_title"]/a/text()').extract_first()
link = item.xpath('div[@class="list_item_title"]/a/@href').extract_first()
pub_date = item.xpath('div[@class="list_item_time"]/text()').extract_first()
# 将数据存储到Item中
yield {
'title': title,
'link': link,
'pub_date': pub_date,
}
```
运行爬虫:
```
scrapy crawl qianlima_spider
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)