简爬新浪新闻网

需要一个chrome浏览器插件,https://chrome.google.com/webstore/detail/infolite/ipjbadabbpedegielkhgpiekdlmfpgal,安装好后打开,可以看到网页块域的类名称




import requests
from bs4 import BeautifulSoup
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:
        time = news.select('.time')[0].text
        h2 = news.select('h2')[0].text
        a = news.select('a')[0]['href']
        print(time,h2,a)
结果:



posted @ 2017-04-08 14:33  Lawliet__zmz  阅读(168)  评论(0编辑  收藏  举报