sina爬虫

本文介绍了一个使用Python爬取新浪新闻网站上特定新闻文章的详细信息的方法,包括标题、发布时间、来源、正文内容、责任编辑以及评论数量。通过正则表达式和requests、BeautifulSoup等库实现了网页内容的抓取和解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

几个坑点,元祖列表字典里的汉字,要一个一个打出来才不会有乱码,否则会有乱码.....或者你用join把它们连接起来就好了,或者少年用python3吧.....

python制表符和空格不能混用,所以如果他告诉你缩进错了,可能就是这个问题,具体怎么查,可以用notepad,看一下制表符和缩进

剩下的,注意注释里的编码

#coding:utf-8
import requests
import re
import sys 
import json                                   #解决python用ascii编码问题
reload(sys)
sys.setdefaultencoding('utf-8')
from datetime import datetime
from bs4 import BeautifulSoup
commentURL='http://comment5.news.sina.com.cn/page/info?version=1&format=js&channel=gn&newsid=comos-{}&group=&compress=0&ie=utf-8&oe=utf-8&page=1&page_size=20'
#format是一种强大的格式化字符串的工具,把id挖走变成{},用format就能填进去任意id
def getCommentCount(newsurl):                    #获取评论函数
	m=re.search('doc-i(.*).shtml',newsurl)    #取出newsurl的newsid
	newsid=m.group(1)
	comments=requests.get(commentURL.format(newsid));
	jd=json.loads(comments.text.strip('var data='))
	return jd['result']['count']['total']

def getNewsDetail(newsurl):
	result={}
	res=requests.get(newsurl)
	res.encoding='utf-8'
	soup=BeautifulSoup(res.text,'html.parser')
	result['title']=soup.select('#artibodyTitle')[0].text
	timesource=soup.select('.time-source')[0].contents[0].strip()
	result['dt']=datetime.strptime(timesource,'%Y年%m月%d日%H:%M')                  #python基于ASCII处理字符的,当出现不属于ASCII的字符时,会出现错误信息。所以代码开头加一个处理
	result['source']=soup.select('.time-source span a')[0].text
	result['article']=''.join([p.text.strip() for p in soup.select('#artibody p')[:-1]])                   #一行写法
	result['editor']=soup.select('.article-editor')[0].text.lstrip('责任编辑:')
	result['comments']=getCommentCount(newsurl)
	return result

if __name__=="__main__":
    result=getNewsDetail('http://news.sina.com.cn/o/2016-12-21/doc-ifxyxvcr7229277.shtml')
    for i in result:
    	print i,result[i]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值