千里马招标爬虫

信息采集(30条)
http://www.qian$$$lima.com/zbgg/
1.可以实现1-5页面的灵活爬取
2.本地txt存储
import requests
from lxml import etree


class WYSpider:

    def __init__(self, p):
        # http://www.qian$$$lima.com/zbgg/p1
        self.host = f'http://www.qian$$$lima.com/zbgg/p{p}'
        self.file_name = f"p{p}"
        self.head = {
            'User-Agent': '用自己的'
        }

    def getHtml(self):
        response = requests.get(url=self.host, headers=self.head)
        response.encoding = 'utf-8'
        if response.status_code == 200:
            return response.content.decode()  # !!!
        else:
            print('请求失败,请重新输入')
            self.getHtml()

    def parseHtml(self, content):
        a = []
        href = []
        et = etree.HTML(content)  # xpath解析
        a_s = et.xpath('//*[@id="__layout"]/div/div[2]/div/div[2]/div[1]/div[1]'
                       '/div[2]/div/div/div/div/div/a[2]')
        print("---", a_s)
        for each in a_s:
            a.append(each.text)
            # a.append(each.text.encode('utf-8').decode('unicode'))  # wrong
        print(a)  # 乱码 ???
        href_s = et.xpath('//*[@id="__layout"]/div/div[2]/div/div[2]/div[1]/div[1]'
                          '/div[2]/div/div/div/div/div/a[2]/@href')
        print(href_s)
        for each in href_s:
            href.append(each)
        return zip(a, href)

    def save(self, a_and_href):
        file = open(self.file_name + ".txt", mode='w', encoding='utf-8')
        for k, v in a_and_href:
            file.write(k + ' : ' + v + '\n')
        file.flush()
        file.close()
        print('END!', file)


for i in range(1, 6):
    app = WYSpider(i)
    app.save(app.parseHtml(app.getHtml()))

 

 

 

 

 

 

 

 

 

 

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
爬取千里马招标网站数据可以使用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 ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值