• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python爬取比比网中标标书并保存成PDF格式

python 搞java代码 3年前 (2022-05-21) 39次浏览 已收录 0个评论

前言

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

PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取

python免费学习资料以及群交流解答点击即可加入

python开发环境

  • python 3.6
  • pycharm
<code class="language-python"><a href="https://www.gaodaima.com/tag/import" title="查看更多关于import的文章" target="_blank">import</a> requests
import parsel
import pdfkit
import time
</code>

www#gaodaima.com来源gao!%daima.com搞$代*!码$网搞代码

相关模块pip安装即可

目标网页分析

1、先从列表页中获取详情页的URL地址

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

<code class="language-python">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)
</code>

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

<code class="language-python">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()
</code>

3、保存html网页数据并转成PDF

<code class="language-python">html_str = """
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
{article}
</body>
</html>
"""
def download(article, title):
    html = html_str.format(article=article)
    html_path = "D:pythondemo招标网文书" + title + ".html"
    pdf_path = "D:pythondemo招标网文书" + 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 Fileswkhtmltopdfinwkhtmltopdf.exe")
    # 把 html 通过 pdfkit 变成 pdf 文件
    pdfkit.from_file(html_path, pdf_path, configuration=config)
</code>

运行实现效果


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python爬取比比网中标标书并保存成PDF格式

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址