python使用bs4的BeautifulSoup解析html

使用 bs4 解析数据:
1.将页面源代码(resp.text)交给 BeautifulSoup 进行处理生成 bs 对象
2.从bs对象中查找数据  find(标签, 属性=值)方法(找第一个) 和 find_all(标签, 属性=值)方法(找全部)import requestsfrom bs4 import BeautifulSoup

 

url = "http://www.xinfadi.com.cn/priceDetail.html"
resp = requests.get(url)
print(resp.content)
# 可以设置响应结果的编码
# resp.encoding = "utf-8"
# 使用 bs4 解析数据
# 1.将页面源代码交给 BeautifulSoup 进行处理生成 bs 对象
bs = BeautifulSoup(resp.text)
# 2.从bs对象中查找数据  find(标签, 属性=值)方法(找第一个) 和 find_all(标签, 属性=值)方法(找全部)
table = bs.find("table")
# id 也可以放在 attrs 中  attrs={"id": "myTh"}   class为python关键字所以用 class_作为 key
thList = table.find_all("th", attrs={}, class_=[]) 
for t in thList:
    # 使用 get方法  获取属性值
    print(t.get("text"))
    # get_text() 获取标签里的内容 eg: <p>just for example </p>  返回 just for example
    print(t.get_text())

 

posted @ 2022-05-07 23:14  不要西红柿  阅读(266)  评论(0编辑  收藏  举报