BeautifulSoup中的一些问题
2020-02-03 02:43
423 查看
使用wkpdftohtml将爬取到的网页生成PDF时,使用示例代码
import requests from bs4 import BeautifulSoup import pdfkit url = 'http://www.liaoxuefeng.com/wiki/' \ '001434446689867b27157e896e74d51a89c25cc8b43bdb3000' response = requests.get(url) bsObj = BeautifulSoup(response.content, 'xml') body = bsObj.findAll(class_='x-wiki-content') html = str(body) html = html.encode('utf-8') fp = open('test.html', 'wb') fp.write(html) fp.close() fp = open('test.pdf', 'wb') fp.close() pdfkit.from_file('test.html', 'test.pdf')
出现警告
Warning: A finished ResourceObject received a loading progress signal. This might be an indication of an iframe taking too long to load
并且没有得到预想中的PDF文件。(代码块访问的是廖雪峰的js教程网站)
将beautifulsoup中的解析器换成"lxml"或“html.parser则能够生成预期的PDF文件
阅读BeautifulSoup的文档
根据beautifulsoup的文档,不同的解析器之前对文本的解析是存在区别的参见beautifulsoup文档
使用xml解析器致使生成的dom树与理想的不同(不是bug,是不同解析器的处理机制),以至于停止生成pdf。
转载于:https://www.cnblogs.com/ZackBee/p/6659849.html
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- 关于BeautifulSoup编码的一些问题
- 关于BeautifulSoup编码的一些问题
- Python Beautifulsoup 自学过程中遇到的一些问题
- VMware中遇到的一些问题
- linux使用中碰到的一些小问题及解决方案-expanding
- cocos2dx-3.5从Mac下移植到windows出现的一些问题
- ubuntu下配置opencv2.4.10 步骤以及一些所遇问题解决方法
- CSS在IE6下的一些BUG和兼容性问题及解决办法
- DNN训练过程中的一些问题以及技巧
- 更新Android Studio 2.3 Beta 2 后一些问题
- Qt的一些坐标相关的问题
- 00-小白关于人工智能的一些问题(不断更新)
- 关于PHP中操作MySQL数据库的一些要注意的问题
- gcc编译参数-fPIC的一些问题
- 使用URLLoader(HttpService)与服务端通信的一些问题-----以php为例
- EXT3整合struts2上传,中间解决了一些问题
- 在使用时SQL Server 2005 Express时的一些问题
- 由最近值类型和引用类型探讨想到的一些问题
- robotium在使用过程中的一些问题和知识点
- 记录一些在Eclipse中遇到的问题