新闻列表页网页结构:
![](https://img.haomeiwen.com/i6661013/f623d07ce3312fbf.png)
输出h2:
res = requests.get('http://news.sina.com.cn/china/')
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text,'html.parser')
for news in soup.select('.news-item'):
if( len(news.select('h2')) >0 ):
print(news.select('h2')[0])
![](https://img.haomeiwen.com/i6661013/5526bd1b0b4c9095.png)
.text
![](https://img.haomeiwen.com/i6661013/162b7e4d610665f8.png)
如法炮制:
获取新闻时间标题超链接
res = requests.get('http://news.sina.com.cn/china/')
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text,'html.parser')
for news in soup.select('.news-item'):
if( len(news.select('h2')) >0 ):
h2 = news.select('h2')[0].text
time = news.select('.time')[0].text
a = news.select('a')[0]['href']
print(time,h2,a)
![](https://img.haomeiwen.com/i6661013/e021d47095512038.png)
注:本文属于原创文章,转载请注明本文地址!
作者QQ:1099718640
CSDN博客主页:http://blog.csdn.net/dyboy2017
Github开源项目:https://github.com/dyboy2017/spider
网友评论