Bs4使用中常遇到哪些常见难题?
- 内容介绍
- 文章标签
- 相关推荐
本文共计185个文字,预计阅读时间需要1分钟。
通过pip安装bs4包:bashpip install bs4
导入包:pythonfrom bs4 import BeautifulSoup
处理页面源代码:pythonpage=BeautifulSoup(resp.text, .parser)
- 2.导入包from bs4 import BeautifulSoup
- 3.把页面源代码交给BeautifulSoup进行处理, 生成bs对象
page = BeautifulSoup(resp.text, "html.parser") # 指定html解析器,如果不指定解析器不会报错,但是会爆红
- 4.从bs对象中查找数据
# find(标签, 属性=值)
# find_all(标签, 属性=值)
- 5.在指定属性的过程中,例如class和id等是python的关键字,所以直接使用python关键字会发现报错,有两种解决方式:
第一种是在关键字后加_可解决问题,例如:class_
table = page.find("table", class_="hq_table")
第二种是使用attrs{},例如:
table = page.find("table", attrs={"class": "hq_table"})
本文共计185个文字,预计阅读时间需要1分钟。
通过pip安装bs4包:bashpip install bs4
导入包:pythonfrom bs4 import BeautifulSoup
处理页面源代码:pythonpage=BeautifulSoup(resp.text, .parser)
- 2.导入包from bs4 import BeautifulSoup
- 3.把页面源代码交给BeautifulSoup进行处理, 生成bs对象
page = BeautifulSoup(resp.text, "html.parser") # 指定html解析器,如果不指定解析器不会报错,但是会爆红
- 4.从bs对象中查找数据
# find(标签, 属性=值)
# find_all(标签, 属性=值)
- 5.在指定属性的过程中,例如class和id等是python的关键字,所以直接使用python关键字会发现报错,有两种解决方式:
第一种是在关键字后加_可解决问题,例如:class_
table = page.find("table", class_="hq_table")
第二种是使用attrs{},例如:
table = page.find("table", attrs={"class": "hq_table"})

