import requests
from bs4 import BeautifulSoup
url = ‘https://www.autohome.com.cn/news/’
ret = requests.get(url=url)
ret.encoding = ‘gbk’
data = ret.text
soup = BeautifulSoup(data, ‘html.parser’)
ul = soup.find(name=‘ul’, attrs={‘class’: ‘article’})
li_list = ul.find_all(name=‘li’)
infos = []
for li in li_list:
name = li.find(name=‘h3’)
name1 = “”
if name:
name1 = (name.text)
href = li.find(name=‘a’)
href1 = “”
if href:
href1 = (‘http:’ + href[‘href’])
info = li.find(name=‘p’)
info1 = “”
if info:
info1 = (info.text)
infos.append({‘title’: name1, ‘href’: href1, ‘info’: info1})
print(infos)
269

被折叠的 条评论
为什么被折叠?



