python 使用BeautifulSoup的 html5lib爬取网站内容

1、使用BeautifulSoup的 'html5lib' 能像网页工具一样渲染内容。
缺点:运行比较慢
2、安装包
1
pip install html5lib
3、直接获取网页的所有有效内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import requests #数据请求模块 第三方模块 pip install requests
from bs4 import BeautifulSoup
heads = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36'
}
def get_response(html_url):
    response = requests.get(url=html_url, headers=heads)
    return response
 
url= 'https://www.python100.com/html/1390.html'
response_new = get_response(html_url=url)
response_new.encoding = 'UTF-8'
#soup = BeautifulSoup(response_new.text, 'html.parser')
soup_new = BeautifulSoup(response_new.text, 'html5lib')
print(soup_new.text)
4、结果展示

 

  

  

posted @   苍茫宇宙  阅读(110)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2022-08-08 tidb自动处理僵尸进程脚本
2020-08-08 CDB、PDB应用
2020-08-08 PDB的创建与删除
2020-08-08 CDB命令方式创建和删除
点击右上角即可分享
微信分享提示