python爬取网页转换为PDF文件
作者:moluchase 时间:2023-02-11 08:48:24
爬虫的起因
官方文档或手册虽然可以查阅,但是如果变成纸质版的岂不是更容易翻阅与记忆。如果简单的复制粘贴,不知道何时能够完成。于是便开始想着将Android的官方手册爬下来。
全篇的实现思路
分析网页
学会使用BeautifulSoup库
爬取并导出
参考资料:
* 把廖雪峰的教程转换为PDF电子书
* Requests文档
* Beautiful Soup文档
配置
在Ubuntu下使用Pycharm运行成功
转PDF需要下载wkhtmltopdf
具体过程
网页分析
如下所示的一个网页,要做的是获取该网页的正文和标题,以及左边导航条的所有网址
接下来的工作就是找到这些标签喽…
关于Requests的使用
详细参考文档,这里只是简单的使用Requests获取html以及使用代理翻墙(网站无法直接访问,需要VPN)
proxies={
"http":"http://vpn的IP:port",
"https":"https://vpn的IP:port",
}
response=requests.get(url,proxies=proxies)
Beautiful Soup的使用
参考资料里面有Beautiful Soup文档,将其看完后,可以知道就讲了两件事:一个是查找标签,一个是修改标签。
本文需要做的是:
1. 获取标题和所有的网址,涉及到的是查找标签
#对标签进行判断,一个标签含有href而不含有description,则返回true
#而我希望获取的是含有href属性而不含有description属性的<a>标签,(且只有a标签含有href)
def has_href_but_no_des(tag):
return tag.has_attr('href') and not tag.has_attr('description')
#网页分析,获取网址和标题
def parse_url_to_html(url):
response=requests.get(url,proxies=proxies)
soup=BeautifulSoup(response.content,"html.parser")
s=[]#获取所有的网址
title=[]#获取对应的标题
tag=soup.find(id="nav")#获取第一个id为"nav"的标签,这个里面包含了网址和标题
for i in tag.find_all(has_href_but_no_des):
s.append(i['href'])
title.append(i.text)
#获取的只是标签集,需要加html前缀
htmls = "<html><head><meta charset='UTF-8'></head><body>"
with open("android_training_3.html",'a') as f:
f.write(htmls)
对上面获取的网址分析,获取正文,并将图片取出存于本地;涉及到的是查找标签和修改属性
#网页操作,获取正文及图片
def get_htmls(urls,title):
for i in range(len(urls)):
response=requests.get(urls[i],proxies=proxies)
soup=BeautifulSoup(response.content,"html.parser")
htmls="<div><h1>"+str(i)+"."+title[i]+"</h1></div>"
tag=soup.find(class_='jd-descr')
#为image添加相对路径,并下载图片
for img in tag.find_all('img'):
im = requests.get(img['src'], proxies=proxies)
filename = os.path.split(img['src'])[1]
with open('image/' + filename, 'wb') as f:
f.write(im.content)
img['src']='image/'+filename
htmls=htmls+str(tag)
with open("android_training_3.html",'a') as f:
f.write(htmls)
print(" (%s) [%s] download end"%(i,title[i]))
htmls="</body></html>"
with open("android_training_3.html",'a') as f:
f.write(htmls)
2.转为PDF
这一步需要下载wkhtmltopdf,在Windows下执行程序一直出错..Ubuntu下可以
def save_pdf(html):
"""
把所有html文件转换成pdf文件
"""
options = {
'page-size': 'Letter',
'encoding': "UTF-8",
'custom-header': [
('Accept-Encoding', 'gzip')
]
}
pdfkit.from_file(html, "android_training_3.pdf", options=options)
最后的效果图
来源:https://blog.csdn.net/molu_chase/article/details/77508260
标签:python,爬取,pdf
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Javascript特效--划过放大的列表
2008-03-21 12:18:00
![](https://img.aspxhome.com/file/UploadPic/20083/21/2008321122917663s.jpg)
python mysql自增字段AUTO_INCREMENT值的修改方式
2023-10-15 07:12:19
![](https://img.aspxhome.com/file/2023/6/70096_0s.jpg)
js string 转 int 注意的问题小结
2024-05-03 15:30:04
Python 计算机视觉编程进阶之OpenCV 图像锐化及边缘检测
2021-07-31 11:03:29
![](https://img.aspxhome.com/file/2023/8/105308_0s.png)
PyQt+socket实现远程操作服务器的方法示例
2022-07-19 01:56:13
![](https://img.aspxhome.com/file/2023/3/107643_0s.png)
python中字符串变二维数组的实例讲解
2021-08-03 04:37:56
![](https://img.aspxhome.com/file/2023/5/76885_0s.jpg)
基于python的列表list和集合set操作
2023-03-20 01:38:34
![](https://img.aspxhome.com/file/2023/6/111876_0s.jpg)
python3 assert 断言的使用详解 (区别于python2)
2023-03-10 03:05:26
IE和Firefox的js兼容性整理
2007-11-21 19:40:00
python操作excel的方法(xlsxwriter包的使用)
2021-10-26 21:47:54
百度、谷歌和雅虎的近日LOGO
2008-05-19 12:11:00
![](https://img.aspxhome.com/file/UploadPic/20085/19/2008519122037724s.gif)
嵌入Flash应该考虑不支持Flash的浏览器
2007-12-20 12:29:00
![](https://img.aspxhome.com/file/UploadPic/200712/20/20071220124122527s.jpg)
如何使用SublimeText3配置 PHP IDE环境
2024-04-30 09:58:51
![](https://img.aspxhome.com/file/2023/9/131619_0s.png)
Pytorch 使用tensor特定条件判断索引
2023-01-18 16:30:23
![](https://img.aspxhome.com/file/2023/8/121158_0s.jpg)
sql 删除表中的重复记录
2024-01-16 01:12:29
wxpython布局的实现方法
2022-01-31 10:20:57
![](https://img.aspxhome.com/file/2023/0/131110_0s.png)
Python操作Mysql实例代码教程在线版(查询手册)
2024-01-24 17:07:44
![](https://img.aspxhome.com/file/2023/6/117986_0s.png)
Python实现二叉树前序、中序、后序及层次遍历示例代码
2023-12-02 00:36:40
最新PyCharm从安装到PyCharm永久激活再到PyCharm官方中文汉化详细教程
2022-09-24 01:38:47
![](https://img.aspxhome.com/file/2023/0/82940_0s.png)
SQL语句练习实例之一——找出最近的两次晋升日期与工资额
2024-01-19 06:19:50