首页 技术 正文
技术 2022年11月7日
0 收藏 916 点赞 705 浏览 1422 个字
# -*- coding: cp936 -*-import urllib,urllib2
from bs4 import BeautifulSoupuser_agent='Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0'
headers={
'User-Agent':user_agent
}
url='https://www.qiushibaike.com'#----------------------
def getcode(url):
req=urllib2.Request(url,headers=headers)
html=urllib2.urlopen(req).read()
soup=BeautifulSoup(html,'html.parser')
content=soup.select('.content')[0].stripped_strings
print '内容:'
for i in content:
print idef getinfo(x):
q=0
for m in range(x): print '获取第%s页'%str(m+1)
murl='https://www.qiushibaike.com/text'+'/page/'+str(m+1) req=urllib2.Request(murl,headers=headers) html=urllib2.urlopen(req,timeout=60).read().decode('utf-8') #创建beautiful对象
#创建本地HTML对象:soup=BeautifulSoup(open('index.html'))
soup=BeautifulSoup(html,'html.parser') #打印HTML内容 print soup.prettify()
authors=soup.select('h2')
article=soup.select('.contentHerf')
jokes=soup.select('.stats-vote > .number')
comment_num=soup.select('.stats-comments > .qiushi_comments > .number') #获取界面上的文字
##print '内容:',article[0].span.string.strip() ##获取链接文字
n=0
for a in article: print '获取第%s页第%s条'%(str(m+1),str(n+1))
print '作者:',authors[n].string.strip()
print '好笑数:',jokes[n].string
article_url=article[n]['href'] article_url=url+article_url
#print article_url
getcode(article_url) print '评论数:',comment_num[n].string
n+=1
q+=1
print '\n'
print '总共%s条数据'%qnu=raw_input('获取多少页:')
nu=int(nu)#设置重新获取数据次数tries=1
try:
getinfo(nu)
except: print u'爬取数据出错。。。'
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918