python爬虫-新闻

作者: nayli | 来源:发表于2019-04-27 20:23 被阅读33次

可能我是个喜欢受虐的人,在大家都开始用python 3,我还坚持使用python 2
o(╥﹏╥)o
看到别人发来的代码,恰好最近对爬虫感兴趣,拿来练练手
一个乱码问题差点让我崩溃,最后发现,只需要将python 2中,将最后一行print()中()去掉即可。。。

python 2

# coding=UTF-8
import requests
from bs4 import BeautifulSoup

res = requests.get('https://news.sina.com.cn/world/')
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 h2,time,a

python 3

import requests
from bs4 import BeautifulSoup

res = requests.get('https://news.sina.com.cn/world/')
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 (h2,time,a)

运行结果

金正恩向俄太平洋舰队战斗荣誉纪念碑献花 4月26日 15:12 https://news.sina.com.cn/o/2019-04-26/doc-ihvhiqax5192175.shtml
特朗普:"通俄门"调查是"政变" 目的是颠覆美政权 4月26日 14:52 https://news.sina.com.cn/o/2019-04-26/doc-ihvhiqax5188103.shtml
金正恩启程返朝 俄仪仗队演奏《喀秋莎》送行 4月26日 14:38 https://news.sina.com.cn/o/2019-04-26/doc-ihvhiewr8375060.shtml
菲律宾东南海域发生5.0级地震 震源深度62.7千米 4月26日 14:37 https://news.sina.com.cn/w/2019-04-26/doc-ihvhiewr8375127.shtml
斯里兰卡总统:警方正就爆炸案追捕140名IS嫌犯 4月26日 14:30 https://news.sina.com.cn/w/2019-04-26/doc-ihvhiqax5185338.shtml
俄罗斯测试漂浮核电站:俄北极开发或迎来新机遇 4月26日 14:29 https://news.sina.com.cn/o/2019-04-26/doc-ihvhiewr8372406.shtml
普京令简化“顿巴斯”居民获俄护照手续 美乌指责 4月26日 14:29 https://news.sina.com.cn/o/2019-04-26/doc-ihvhiewr8373072.shtml
英国威廉王子访新西兰恐袭案发地 将会见幸存者 4月26日 14:25 https://news.sina.com.cn/w/2019-04-26/doc-ihvhiewr8372466.shtml
40名外国公民在斯里兰卡爆炸中遇难 仍有14人失联 4月26日 14:25 https://news.sina.com.cn/c/2019-04-26/doc-ihvhiewr8372179.shtml
朝鲜领导人金正恩登上返程专列 承诺将再次访俄 4月26日 14:22 https://news.sina.com.cn/w/2019-04-26/doc-ihvhiewr8372003.shtml
经费充足就能拯救圣母院?美媒:还需21世纪的技术 4月26日 14:19 https://news.sina.com.cn/o/2019-04-26/doc-ihvhiewr8370723.shtml
斯里兰卡警察局长辞职 总统:很快将提名新人选 4月26日 14:14 https://news.sina.com.cn/w/2019-04-26/doc-ihvhiqax5181764.shtml
韩国300多名议员大打出手 锤子铁棍轮番撬门(图) 4月26日 14:13 https://news.sina.com.cn/w/2019-04-26/doc-ihvhiqax5182871.shtml
马克龙再安抚“黄背心”:减税50亿欧 但延长工时 4月26日 14:09 https://news.sina.com.cn/o/2019-04-26/doc-ihvhiewr8369646.shtml
斯里兰卡总统:安全机构不尽职 将进行全面重组 4月26日 14:08 https://news.sina.com.cn/c/2019-04-26/doc-ihvhiqax5180929.shtml
朝鲜领导人金正恩已启程回国 结束对俄首次访问 4月26日 13:58 https://news.sina.com.cn/w/2019-04-26/doc-ihvhiewr8369255.shtml
菲律宾东南海域发生5.2级地震 震源深度67.4公里 4月26日 13:54 https://news.sina.com.cn/w/2019-04-26/doc-ihvhiewr8367410.shtml
韩媒:金正恩离开俄罗斯 结束三天两夜访俄日程 4月26日 13:46 https://news.sina.com.cn/w/2019-04-26/doc-ihvhiqax5178372.shtml
调查显示德国逾一半受访者对难民持负面态度 4月26日 13:06 https://news.sina.com.cn/w/2019-04-26/doc-ihvhiqax5171641.shtml
法“国民联盟”主席称“中国是抄袭造假的行家” 4月26日 12:59 https://news.sina.com.cn/w/2019-04-26/doc-ihvhiewr8360407.shtml
印度18名学生自杀 疑因系统故障致成绩显示不合格 4月26日 12:50 https://news.sina.com.cn/w/2019-04-26/doc-ihvhiewr8365225.shtml
伊朗外长欲寻与美合作可能 喊话特朗普别掉入陷阱 4月26日 12:40 https://news.sina.com.cn/w/2019-04-26/doc-ihvhiqax5167671.shtml
马蒂斯受特朗普冷眼相待 美在中期选举后换防长? 9月17日 05:34 http://news.sina.com.cn/w/sy/2018-09-17/doc-ifxeuwwr5026118.shtml
安倍想签和平协议 普京:年底前可以 但需不设条件 9月12日 14:56 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiixzkm7789304.shtml
阿富汗自杀式爆炸已致68死165伤 塔利班否认犯案 9月12日 14:49 http://news.sina.com.cn/w/2018-09-12/doc-ihiixyeu6570798.shtml
希腊总理:不会重蹈致希腊濒临金融崩溃的覆辙 9月12日 14:37 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiycyfx7926551.shtml
俄副外长:莫斯科将呼吁联合国安理会放宽对朝制裁 9月12日 14:31 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiycyfx7891758.shtml
朝鲜的农业改革进展如何?记者实地探访 9月12日 14:27 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiycyfx7869435.shtml
北海道地震后日本网络谣言满天飞 政党账号也乌龙 9月12日 14:07 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiycyfx7752789.shtml
美将售日“先进鹰眼”预警机 却无法与美共享数据 9月12日 13:49 http://news.sina.com.cn/o/2018-09-12/doc-ihiycyfx7662621.shtml
日本政府指示调查北海道大面积停电 呼吁继续节电 9月12日 13:46 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiycyfx7651824.shtml
伊朗希望核协议能存续 警告称正策划境外打击活动 9月12日 13:40 http://news.sina.com.cn/w/2018-09-12/doc-ihiycyfx7618273.shtml
30年最强飓风来了 要撤离但这个大学却偏要这么做 9月12日 13:27 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiycyfx7561971.shtml
印度发生5.4级地震 距我国边境线最近约147公里 9月12日 13:19 http://news.sina.com.cn/o/2018-09-12/doc-ihiycyfx7521704.shtml
美国务院:巴解组织驻华盛顿办事处可有条件重开 9月12日 13:10 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiycyfx7481018.shtml
南非2017年日均遇害人数近60 警察部长:堪比战区 9月12日 11:44 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiixzkm7712663.shtml
韩国10名疑中东呼吸综合征感染者检测阴性 被监控 9月12日 11:26 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiycyfx6811673.shtml
没有凯撒的魅力 特朗普抓内鬼只会巩固反抗传统 9月12日 11:11 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiixzkm7698428.shtml
强飓风即将登陆 特朗普:非常大 我们完全准备好了 9月12日 11:10 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiixyeu6483334.shtml
日本参议员猪木提议朝方接受日本议员团访朝 9月12日 10:49 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiycyfx6479052.shtml
法国女子教唆年轻女性加入IS 被判7年监禁 9月12日 10:49 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiycyfx6477059.shtml
利比亚首都机场遭炮击 尚无人员伤亡财产损失报告 9月12日 10:44 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiycyfx6435498.shtml
马蒂斯受特朗普冷眼相待 美在中期选举后换防长? 9月17日 05:34 http://news.sina.com.cn/w/sy/2018-09-17/doc-ifxeuwwr5026118.shtml
安倍想签和平协议 普京:年底前可以 但需不设条件 9月12日 14:56 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiixzkm7789304.shtml
阿富汗自杀式爆炸已致68死165伤 塔利班否认犯案 9月12日 14:49 http://news.sina.com.cn/w/2018-09-12/doc-ihiixyeu6570798.shtml
希腊总理:不会重蹈致希腊濒临金融崩溃的覆辙 9月12日 14:37 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiycyfx7926551.shtml
俄副外长:莫斯科将呼吁联合国安理会放宽对朝制裁 9月12日 14:31 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiycyfx7891758.shtml
朝鲜的农业改革进展如何?记者实地探访 9月12日 14:27 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiycyfx7869435.shtml
北海道地震后日本网络谣言满天飞 政党账号也乌龙 9月12日 14:07 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiycyfx7752789.shtml
美将售日“先进鹰眼”预警机 却无法与美共享数据 9月12日 13:49 http://news.sina.com.cn/o/2018-09-12/doc-ihiycyfx7662621.shtml
日本政府指示调查北海道大面积停电 呼吁继续节电 9月12日 13:46 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiycyfx7651824.shtml
伊朗希望核协议能存续 警告称正策划境外打击活动 9月12日 13:40 http://news.sina.com.cn/w/2018-09-12/doc-ihiycyfx7618273.shtml
30年最强飓风来了 要撤离但这个大学却偏要这么做 9月12日 13:27 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiycyfx7561971.shtml
印度发生5.4级地震 距我国边境线最近约147公里 9月12日 13:19 http://news.sina.com.cn/o/2018-09-12/doc-ihiycyfx7521704.shtml
美国务院:巴解组织驻华盛顿办事处可有条件重开 9月12日 13:10 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiycyfx7481018.shtml
南非2017年日均遇害人数近60 警察部长:堪比战区 9月12日 11:44 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiixzkm7712663.shtml
韩国10名疑中东呼吸综合征感染者检测阴性 被监控 9月12日 11:26 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiycyfx6811673.shtml
没有凯撒的魅力 特朗普抓内鬼只会巩固反抗传统 9月12日 11:11 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiixzkm7698428.shtml
强飓风即将登陆 特朗普:非常大 我们完全准备好了 9月12日 11:10 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiixyeu6483334.shtml
日本参议员猪木提议朝方接受日本议员团访朝 9月12日 10:49 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiycyfx6479052.shtml
法国女子教唆年轻女性加入IS 被判7年监禁 9月12日 10:49 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiycyfx6477059.shtml
利比亚首都机场遭炮击 尚无人员伤亡财产损失报告 9月12日 10:44 http://news.sina.com.cn/w/sy/2018-09-12/doc-ihiycyfx6435498.shtml
朝鲜又搞核试验 该拿他怎么办? 9月9日 19:45 http://news.sina.com.cn/w/2016-09-09/doc-ifxvukhv7966689.shtml
韩民众抗议萨德部署拦总理专车 7月15日 14:58 http://news.sina.com.cn/w/2016-07-15/doc-ifxuapvs8512683.shtml
韩防长称萨德只是个“炮兵连” 7月12日 16:21 http://news.sina.com.cn/w/2016-07-12/doc-ifxtwihq0110439.shtml
韩媒:韩美就萨德部署已提前向中俄说明 7月8日 12:24 http://news.sina.com.cn/w/2016-07-08/doc-ifxtwihp9817581.shtml
韩媒:韩将在韩朝边境增加“大喇叭” 7月6日 16:53 http://news.sina.com.cn/w/2016-07-06/doc-ifxtsatm1509716.shtml
日媒:美军向南海派出三艘宙斯盾驱逐舰 6月25日 14:21 http://news.sina.com.cn/w/2016-06-25/doc-ifxtmwei9294295.shtml
日本福山市因大雨发生决堤 政府出动泵车排水 6月24日 13:17 http://news.sina.com.cn/o/2016-06-24/doc-ifxtmwri4380620.shtml
里约奥运火炬传递“请”美洲虎参加 后射杀 6月22日 16:20 http://news.sina.com.cn/w/2016-06-22/doc-ifxtfrrc4136284.shtml
中国军舰进入日领海后 海警又巡航钓鱼岛 6月15日 21:17 http://news.sina.com.cn/w/2016-06-15/doc-ifxtfrrc3645587.shtml
东京都知事引咎辞职 被控挪用“巨额”公款 6月15日 13:28 http://news.sina.com.cn/w/zx/2016-06-15/doc-ifxszmaa2050389.shtml
日媒:中国军舰今天凌晨“侵入”日本领海 6月15日 11:49 http://news.sina.com.cn/w/zx/2016-06-15/doc-ifxszkzy5314714.shtml
越南选举产生新国会代表 7月选举新国家领导人 6月10日 01:00 http://news.sina.com.cn/w/sy/2016-06-10/doc-ifxszmnz6955431.shtml
美媒:中国巨型半潜船下水 打造海上支援平台 6月9日 09:17 http://news.sina.com.cn/c/2016-06-09/doc-ifxszfak3446744.shtml
美日印将在中国附近海上演习 牵制中方东海行动 6月9日 09:17 http://news.sina.com.cn/c/2016-06-09/doc-ifxszfak3446739.shtml
美国六艘航母齐出动对华施压 为菲律宾撑腰 6月8日 08:17 http://news.sina.com.cn/c/2016-06-08/doc-ifxsuypf5208238.shtml
泰国虎庙黑幕调查:抢走幼虎催雌虎发情再生产 6月3日 21:30 http://news.sina.com.cn/w/sy/2016-06-04/doc-ifxsuypf4909057.shtml
霍金评价特朗普:他是个煽动家 5月31日 19:23 http://news.sina.com.cn/w/zx/2016-05-31/doc-ifxsqxxu4804515.shtml
日政府公布安倍外访花费:92国87亿日元 5月31日 17:28 http://news.sina.com.cn/w/zx/2016-05-31/doc-ifxsqxxu4786973.shtml
日本在野党集体向安倍内阁提出不信任案 5月31日 15:03 http://news.sina.com.cn/w/zx/2016-05-31/doc-ifxsqxxu4763329.shtml
奥巴马G7评特朗普赢初选:让首脑们“惊慌” 5月27日 19:43 http://news.sina.com.cn/w/zx/2016-05-27/doc-ifxsqxxu4553977.shtml
外媒:意大利地震造成至少14人死亡 8月24日 16:07 http://news.sina.com.cn/w/zx/2016-08-24/doc-ifxvcsrm2406801.shtml
法渔船在英海域“网到”一艘葡萄牙潜艇 7月13日 16:43 http://news.sina.com.cn/w/2016-07-13/doc-ifxtwiht3709827.shtml
战机事件后首次 土总统与普京约在杭州见面 6月30日 12:30 http://news.sina.com.cn/w/2016-06-30/doc-ifxtsatn7815615.shtml
英国确定脱欧引发亚洲股市大幅震荡 6月24日 13:58 http://news.sina.com.cn/w/zx/2016-06-24/doc-ifxtmweh2468204.shtml
英国确定脱欧 专家:短期负面冲击人民币汇率 6月24日 13:22 http://news.sina.com.cn/w/sd/2016-06-24/doc-ifxtmweh2465886.shtml
“脱欧”或成定局 英国内部或分崩离析 6月24日 13:14 http://news.sina.com.cn/w/sd/2016-06-24/doc-ifxtmwei9231805.shtml
英国退欧:欧洲一体化的一次不可逆的重创 6月24日 13:11 http://news.sina.com.cn/w/sd/2016-06-24/doc-ifxtmwei9231694.shtml
英国几可确定脱欧 6月24日 13:04 http://news.sina.com.cn/w/zx/2016-06-24/doc-ifxtmweh2465070.shtml
英国脱欧公投 新浪观察员英法德三国直播 6月23日 09:23 http://news.sina.com.cn/w/2016-06-23/doc-ifxtmwei9124573.shtml
欧洲杯英俄踢平 俄球迷满场追打英国人…… 6月12日 13:20 http://news.sina.com.cn/w/2016-06-12/doc-ifxszkzy5132781.shtml
日媒:苏宁买了国际米兰又能如何 6月7日 19:11 http://news.sina.com.cn/w/zx/2016-06-07/doc-ifxsvenv6844360.shtml
巴黎恐袭嫌犯欲淡化罪行 首次受审拒不开口 5月21日 10:36 http://news.sina.com.cn/w/sy/2016-05-21/doc-ifxsktkp9093284.shtml
为防性侵案 德国卫生部为难民设立性教育网站 3月13日 20:10 http://news.sina.com.cn/w/zx/2016-03-13/doc-ifxqhmve9136800.shtml
俄罗斯超过百人死于流感疫情 医用口罩脱销 1月29日 01:00 http://news.sina.com.cn/o/2016-01-29/doc-ifxnzani6980419.shtml
科隆抗议性侵游行现第二帝国旗帜(图) 1月11日 14:34 http://news.sina.com.cn/w/sy/2016-01-11/doc-ifxnkkux1105743.shtml
英国统计局为英国估价:共值八万亿英镑 12月28日 03:40 http://news.sina.com.cn/o/2015-12-28/doc-ifxmxxsr3837773.shtml
英国宇航员在太空拨错电话:这是地球吗? 12月28日 03:40 http://news.sina.com.cn/o/2015-12-28/doc-ifxmxxsr3837772.shtml
组图:塞尔维亚军 美女最多的军队 12月25日 11:14 http://news.sina.com.cn/2015-12-25/doc-ifxmxxsp6917523.shtml
组图:英男子遭朋友恶搞扒裤子倒地 12月24日 10:32 http://slide.news.sina.com.cn/w/slide_1_2841_93252.html
组图:俄今年国内十大搞笑事儿都有啥 12月24日 09:37 http://slide.news.sina.com.cn/w/slide_1_2841_93248.html
特朗普对华态度缓和 或不将中国列为货币操纵国 3月14日 15:58 http://news.sina.com.cn/w/zx/2017-03-14/doc-ifychhus1294929.shtml
美国大选决战日 主角是他们 11月7日 16:47 http://news.sina.com.cn/w/zx/2016-11-07/doc-ifxxnety7590573.shtml
外媒:奥巴马正研究卸任后成为一家NBA球队老板 6月25日 09:59 http://news.sina.com.cn/w/hq/2016-06-25/doc-ifxtmweh2528492.shtml
美军集结两艘航母 菲附近海域练兵 6月20日 15:47 http://news.sina.com.cn/w/2016-06-20/doc-ifxtfrrc3955260.shtml
美媒:在美国买支枪比养宠物还容易 6月17日 15:47 http://news.sina.com.cn/w/zx/2016-06-17/doc-ifxtfrrc3769522.shtml
美海军最高官员南海登航母 称美军保障南海和平 6月9日 09:17 http://news.sina.com.cn/c/2016-06-09/doc-ifxszmnz6926526.shtml
希拉里锁定民主党总统候选人提名 将对决特朗普 6月8日 08:20 http://news.sina.com.cn/o/2016-06-08/doc-ifxsuypf5208464.shtml
秘鲁总统大选结束  没人宣布“获胜” 6月7日 17:42 http://news.sina.com.cn/w/zx/2016-06-07/doc-ifxsvenx3567587.shtml
拳王阿里逝世 享年74岁 6月4日 12:31 http://news.sina.com.cn/2016-06-04/doc-ifxsvenv6501157.shtml
安检让乘客等太久 美交通局高官被停职 5月25日 18:31 http://news.sina.com.cn/w/zx/2016-05-25/doc-ifxsktkp9380061.shtml
桑德斯击败希拉里赢得西弗吉尼亚州初选 5月11日 11:58 http://news.sina.com.cn/w/zx/2016-05-11/doc-ifxryhhh1916842.shtml
特朗普再向韩日要“保护费”:不给钱就撤兵 5月5日 11:49 http://news.sina.com.cn/w/zx/2016-05-05/doc-ifxryhhh1622390.shtml
自称掌握宇宙秘密 男子绑“炸弹”逼电视台播出 4月29日 17:07 http://news.sina.com.cn/w/zx/2016-04-29/doc-ifxrtztc3054243.shtml
奥巴马凭啥提前看完《权力的游戏》第六季? 4月25日 21:23 http://news.sina.com.cn/w/zx/2016-04-25/doc-ifxrpvcy4462374.shtml
20美元纸币印上女性头像“惹到”特朗普 4月22日 15:06 http://news.sina.com.cn/w/2016-04-22/doc-ifxrpvea1103524.shtml
美国五角大楼请人“自黑” 黑掉网站者有奖 3月4日 03:19 http://news.sina.com.cn/o/2016-03-04/doc-ifxpzzhk2121925.shtml
特朗普妻子谈成为第一夫人:已开始为以后做准备 3月4日 01:19 http://news.sina.com.cn/w/2016-03-04/doc-ifxqafrm6826856.shtml
墨毒枭接受好莱坞影星专访 当局据此将其抓获 1月11日 08:48 http://news.sina.com.cn/c/nd/2016-01-11/doc-ifxnkkuy7857608.shtml
CES科技峰会全球关注 苹果为何偏不搭理? 1月8日 10:24 http://news.sina.com.cn/zg/world/2016-01-08/doc-ifxnkkuv4196997.shtml
组图:巴西真人芭比走红 细腰32F巨胸受追捧 12月18日 15:16 http://slide.news.sina.com.cn/w/slide_1_2841_92988.html

相关文章

网友评论

    本文标题:python爬虫-新闻

    本文链接:https://www.haomeiwen.com/subject/vfqqnqtx.html