基本信息
源码名称:python 获取新浪网新闻链接
源码大小:0.71KB
文件格式:.py
开发语言:Python
更新时间:2017-09-14
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

通过python BS 模块获取新浪新闻 链接,

如果您的python版本较高(>=python 3.3),那么请按照如下注释

#reload(sys)
#sys.setdefaultencoding("utf-8")
import imp
imp.reload(sys)

抓取结果保存在sinonews.txt中,如下:

#coding = 'utf-8'

import requests
from bs4 import BeautifulSoup
import sys
#reload(sys)
#sys.setdefaultencoding("utf-8")
import imp
imp.reload(sys)


url = "http://news.sina.com.cn/china/"
res = requests.get(url)
res.encoding = 'utf-8'


soup = BeautifulSoup(res.text, "html.parser")
elements = soup.select('.news-item')
    


fname = "sinanews.txt"
try:
    f = open(fname, 'w')
    for element in elements:
        if len(element.select('h2')) > 0:
            f.write(element.select('.time')[0].text)
            f.write(element.select('h2')[0].text)
            f.write(element.select('a')[0]['href'])
            f.write('\n\n')
    

    f.close()
except Exception as  e:
    print(e) 
else:
    pass
finally:
    pass