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

 2023-09-25 阅读 42 评论 0

摘要:前言本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。python开发环境python3,python 3.6pycharmrequestsparselpython提取pdf文件内容、pdfkittime相关模块pip安装即可目标网页分析python生成pdf文件?1、先从列表页中获

前言

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。

python开发环境

python3,python 3.6

pycharm

requests

parsel

python提取pdf文件内容、pdfkit

time

相关模块pip安装即可

目标网页分析

d382d3d6695e2036dd88bf8452d3ec49.png

python生成pdf文件?1、先从列表页中获取详情页的URL地址

是静态网站,可以直接请求网页获取数据

91e8ebced4f982065ec4fb33c1e633c7.png

for page in range(1, 31):

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

python生成pdf报告?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'

}

python word转pdf、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:

python为什么叫爬虫,print(page_url)

2、从详情页中获取标题以及内容

#Python学习交流QQ群:778463939

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

python生成pdf,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)

运行实现效果

3203086cb692fdb4b9d25cd987926f45.png

8ae1cfb52c1ba50efdfc05ed7702c3fd.png

2f94cebdf761af0279cc42dbe2bbbbf4.png

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/100100.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息