selecto内容寻找方法:
1)打开Chrome浏览器,鼠标定位在需要提取的数据,右击,在弹出的快捷菜单中选择"检查"命令。
2)在网页源码中右击所选元素。
3)在弹出的快捷菜单中选着Copy selector。
import requests
from bs4 import BeautifulSoup
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
}
res = requests.get("http://bj.xiaozhu.com/",headers=headers)
html = res.text
soup = BeautifulSoup(html,"html.parser")
princes = soup.select("#page_list > ul > li > div.result_btm_con.lodgeunitname > div:nth-child(1) > span > i")
for prince in princes:
print(prince.get_text())
注意:提取的数据带有标签,这时就可以用**get_text()**方法获取标签数据
而get(“标签属性名”) 获得相应的标签属性信息