from bs4 import BeautifulSoup print("path-",BeautifulSoup) import requests headers = { "User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36" } response_get = requests.get(url="http://www.qianlima.com/zb/area_305/",headers=headers) print("code-",response_get.status_code) # 设置编码格式 response_get.encoding = "utf-8" # 获取返回数据 html = response_get.text # print(html) # 实例话BS对象 1 数据源 2 解析器 soup = BeautifulSoup(html,"html.parser") # 1.Tag对象:可以通过tagname获取该文档中第一次出现的标签 title = soup.title print(title,type(title)) a = soup.a print(a,type(a)) img = soup.img print(img,type(img)) # 2.Tag对象内容操作 # text:只能获取自己标签内部的内容 # string:获取自己标签内部和子级标签的内容 # 没有内容:返回Neoe,不能继续操作,否则报异常 print(title.text) print(a.string) # print(img.string.strip()) # AttributeError: 'NoneType' object has no attribut
BeautifulSoup
最新推荐文章于 2022-09-30 18:01:13 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)