beautifulsoup爬取糗事百科
# _*_ coding:utf-8 _*_
import urllib2
from bs4 import BeautifulSoup user_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"
# 爬取article链接的content内容
def getContent(article_url, headers):
request = urllib2.Request(article_url,data=None,headers=headers)
response = urllib2.urlopen(request,timeout=60)
html = response.read().decode('utf-8')
soup = BeautifulSoup(html,'html.parser')
contents= soup.select('.content')[0].strings
print u"内容:"
for content in contents:
print u"%s" % content.strip()
print '\n'
# ----------------------------
# 获取"https://www.qiushibaike.com/hot/page/1/"页面的作者、好笑、评论,文章地址信息
def getData(url, headers,pages=1):
for page in range(1,pages+1):
page_url = url + "/hot/page/" + str(page)
print "正在爬取第 %s 页+++" % page
request = urllib2.Request(page_url,data=None,headers=headers)
response = urllib2.urlopen(request,timeout=60)
html = response.read().decode('utf-8')
# print html
soup = BeautifulSoup(html,'html.parser') authors = soup.select('h2')
smile_nums = soup.select('.stats-vote > .number')
comment_nums = soup.select('.stats-comments > .qiushi_comments > .number')
article_urls = soup.select('.contentHerf')
for i in range(25):
print "正在爬取第 %s 页的第 %s 条数据---" % (page,i+1)
author = authors[i].string.strip()
print u"作者: %s" % author
funny_num = smile_nums[i].string
comment_num = comment_nums[i].string
print u"好笑: %s" % funny_num
print u"评论: %s"% comment_num
article_url = article_urls[i]['href'] article_url = url + article_url
# print article_url
getContent(article_url, headers)
# ---------------------------------
getData(url,headers,pages=10)
beautifulsoup爬取糗事百科的更多相关文章
- python_爬虫一之爬取糗事百科上的段子
目标 抓取糗事百科上的段子 实现每按一次回车显示一个段子 输入想要看的页数,按 'Q' 或者 'q' 退出 实现思路 目标网址:糗事百科 使用requests抓取页面 requests官方教程 使用 ...
- python爬虫之爬取糗事百科并将爬取内容保存至Excel中
本篇博文为使用python爬虫爬取糗事百科content并将爬取内容存入excel中保存·. 实验环境:Windows10 代码编辑工具:pycharm 使用selenium(自动化测试工具)+p ...
- 8.Python爬虫实战一之爬取糗事百科段子
大家好,前面入门已经说了那么多基础知识了,下面我们做几个实战项目来挑战一下吧.那么这次为大家带来,Python爬取糗事百科的小段子的例子. 首先,糗事百科大家都听说过吧?糗友们发的搞笑的段子一抓一大把 ...
- python网络爬虫--简单爬取糗事百科
刚开始学习python爬虫,写了一个简单python程序爬取糗事百科. 具体步骤是这样的:首先查看糗事百科的url:http://www.qiushibaike.com/8hr/page/2/?s=4 ...
- python学习(十六)写爬虫爬取糗事百科段子
原文链接:爬取糗事百科段子 利用前面学到的文件.正则表达式.urllib的知识,综合运用,爬取糗事百科的段子先用urllib库获取糗事百科热帖第一页的数据.并打开文件进行保存,正好可以熟悉一下之前学过 ...
- 16-多线程爬取糗事百科(python+Tread)
https://www.cnblogs.com/alamZ/p/7414020.html 课件内容 #_*_ coding: utf-8 _*_ ''' Created on 2018年7月17日 ...
- Python爬虫实战一之爬取糗事百科段子
大家好,前面入门已经说了那么多基础知识了,下面我们做几个实战项目来挑战一下吧.那么这次为大家带来,Python爬取糗事百科的小段子的例子. 首先,糗事百科大家都听说过吧?糗友们发的搞笑的段子一抓一大把 ...
- python爬取糗事百科段子
初步爬取糗事百科第一页段子(发布人,发布内容,好笑数和评论数) #-*-coding:utf--*- import urllib import urllib2 import re page = url ...
- 转 Python爬虫实战一之爬取糗事百科段子
静觅 » Python爬虫实战一之爬取糗事百科段子 首先,糗事百科大家都听说过吧?糗友们发的搞笑的段子一抓一大把,这次我们尝试一下用爬虫把他们抓取下来. 友情提示 糗事百科在前一段时间进行了改版,导致 ...
随机推荐
- spring boot 常见的第三方集成
spring boot基于1.x. 一 集成redis 1.1 配置 spring.redis.host = localhost spring.redis.port = 6379 spring.red ...
- android HTTP镜像
mirrors.neusoft.edu.cn 80
- python包的一些问题
1查看python包的版本 2 卸载包的方法 3指定python包的安装版本 参考网址 https://blog.csdn.net/colourful_sky/article/details/8018 ...
- java中比较特殊的三个浮点数Infinity、-Infinity、NaN
学过javaScript的应该都知道,在js中的数值型number类型中有几个特殊的数,一个正无穷大.一个负无穷大.一个不是一个数NaN. 后来无意中发现java中也有这三个数,不过这三个数是浮点数, ...
- SPA
为什么用SPA 1. 减少服务器压力 如果不用spa 那么每次切换页面的时候,就会向服务器发送一个请求 服务器返回一个html文件 如果使用了SPA 在切换时,不需要请求服务器,只要通过本地 ...
- xmlhttp js 请求
<html> <script> var xhr = new XMLHttpRequest(); xhr.open('GET', "http://ipinfo.io/j ...
- js真乃神器也
var a =document.getElementsByClassName('SearchResult__title-text'); for (i = 0;i < a.length;i++){ ...
- System program tools
program 描述 RSBDCOS0 执行 OS 命令 (已登录在 SYSLOG 和跟踪文件中)
- 2019新版UI设计面试题汇总(附答案)
问题一.Android手机的常用设计尺寸有_________.怎么适配ios和安卓. 答案:安卓320 X 480是常规模拟器.但现在的开发都是用360x640做一倍率.480 X 800(1.5倍率 ...
- 如何将一个excel表格的数据匹配到另一个表中
我们在操作excel表的时,有时需要将一个excel表中的数据匹配到另一个表中,那么就需要用到VLOOKUP函数,VLOOKUP函数是Excel中的一个纵向查找函数,VLOOKUP是按列查找,最终返回 ...