import requests
from bs4 import BeautifulSoup
if __name__ == "__main__":
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36 QQBrowser/4.4.106.400',
'Connection': 'keep-alive',
'Accept-Language': 'zh-CN,zh;q=0.9',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'}
html=requests.get('https://car.autohome.com.cn/price/brand-33.html',headers=headers)
html.encoding = 'gb2312'
soup = BeautifulSoup(html.text,'lxml')
#
xx=soup.find('h2','class_=fn-left name')
yy=xx.find('a')
print(yy['href']) #出错,如何能取到这个h2中的汽车品牌名称呢?
'''
<h2 class="fn-left name"><a href="/price/brand-33.html">奥迪</a></h2>
'''```
添加回答
举报
0/150
提交
取消