Python爬虫爬取汽车之家新闻资讯墨颜丶2019-12-182024-11-1312345678910111213141516171819202122232425262728293031323334## requests+Beautifulsoup爬取汽车之家新闻import requestsfrom bs4 import BeautifulSoupresponse=requests.get('https://www.autohome.com.cn/news/')response.encoding='gbk'with open('a.html','w',encoding='utf-8') as f: f.write(response.text)soup=BeautifulSoup(response.text,'lxml')news=soup.find(id='auto-channel-lazyload-article').select('ul li a')for tag in news: link=tag.attrs['href'] imag=tag.select('.article-pic img')[0].attrs['src'] title=tag.find('h3').get_text() sub_time=tag.find(class_='fn-left').get_text() browsing_num=tag.select('.fn-right em')[0].get_text() comment=tag.find('p').get_text() msg=''' ====================================== 链接:http:%s 图片:http:%s 标题:%s 发布时间:%s 浏览数:%s 介绍:%s ''' %(link,imag,title,sub_time,browsing_num,comment) print(msg)