本文的文字及图片来源于网络,仅供学习、交流使用,不具备任何商业用途,若有问题请及时联系咱们以做处理。css
PS:若有须要Python学习资料的小伙伴能够加点击下方连接自行获取html
python免费学习资料以及群交流解答点击便可加入python
python开发环境网络
import requests import parsel import pdfkit import time
相关模块pip安装便可学习
一、先从列表页中获取详情页的URL地址网站
是静态网站,能够直接请求网页获取数据
url
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
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()
三、保存html网页数据并转成PDForm
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:\\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)