python制作标书_Python爬取比比网中标标书并保存成PDF格式

本文介绍了如何使用Python爬虫从比比网抓取免费标书的详细步骤,包括获取标书列表页URL,解析详情页获取标题和内容,最后将HTML内容保存为PDF文件。
摘要由CSDN通过智能技术生成

序言

文中的文本及图片来自互联网,仅作学习培训、沟通交流应用,不具备一切商业行为,如有什么问题请立即在线留言以作解决。

PS:若有必须Python学习材料的小伙伴们能够加点击正下方连接自主获得

python免费学习材料及其群沟通交流解释点击就可以添加

python开发工具

python 3.6

pycharm

import requests

import parsel

import pdfkit

import time

有关控制模块pip安装就可以

总体目标网页分析

1、先从列表页中获得宝贝详情的URL详细地址

是静态网页,能够立即要求网页页面读取数据

0277c96298e2d10fefe36ee0064f27ff.png

for page in range(1, 31):

url = 'https://www.bibenet.com/mfzbu{}.html'.format(page)

headers = {

'Referer': 'https://www.bibenet.com/mianfei/',

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'

}

response = requests.get(url=url, headers=headers)

selector = parsel.Selector(response.text)

urls = selector.css('body > div.wrap > div.clearFiex > div.col9.fl > div.secondary_box > table tr .fl a::attr(href)').getall()

for page_url in urls:

print(page_url)

2、从宝贝详情中获得题目及其內容

response_2 = requests.get(url=page_url, headers=headers)

selector_2 = parsel.Selector(response_2.text)

article = selector_2.css('.container').get()

title = selector_2.css('.detailtitle::text').get()

3、储存html网页页面数据信息并转为PDF

html_str = """

Document

{article}

"""

def download(article, title):

html = html_str.format(article=article)

html_path = 'D:\\python\\demo\\招标网\\公文\\' title '.html'

pdf_path = 'D:\\python\\demo\\招标网\\公文\\' title '.pdf'

with open(html_path, mode='wb', encoding='utf-8') as f:

f.write(html)

print('{}已下载进行'.format(title))

# exe 文档储放的途径

config = pdfkit.configuration(wkhtmltopdf='C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe')

# 把 html 根据 pdfkit 变为 pdf 文档

pdfkit.from_file(html_path, pdf_path, configuration=config)

运作完成实际效果

16fe5c6d3790b319c30ab55f08498853.png

04b62a3c0525b333fbd3105aae4c0c1a.png

ef4f38281d4e00cd23bbc45752b2a80a.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值