【Python3.6】糗事百科爬虫
来源:互联网 发布:淘宝ins同款什么意思 编辑:程序博客网 时间:2024/06/05 04:53
代码链接:https://gitee.com/AI-Echo/codes/kox2aqn4jwi8c6b70l91e25
糗百段子在这样的div标签中,
#!/usr/bin/env python3# -*- coding: utf-8 -*-"""Spyder EditorThis is a temporary script file."""import requestsfrom bs4 import BeautifulSoup as bs#检查url是否为有效链接(以后补)#读取html页面def getHtml(parm): #加上headers,伪装成浏览器,因为有的网站有反爬措施 Headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'} url='https://www.qiushibaike.com/hot/page/'+str(parm) #request对象,发送一个request请求 reponse=requests.get(url,headers=Headers) #返回reponse,读取服务器响应的内容,可通过reponse.encoding指定编码格式 html=reponse.text return html#解析html页面def parseHtml(parm): html=getHtml(parm) soup = bs(html,"html.parser") #新建BeautifulSoup对象 #find_all用法: #soup.find_all("p", "title") #返回值:[<p class="title"><b>The Dormouse's story</b></p>] cont=soup.find_all('div','content') #print(soup.find_all('div','content')) items=[] for x in cont: #get_text()方法:用来获取标签里面的文本内容,在括号里面加"strip=True"可以去除文本前后多余的空格 items.append(x.get_text(strip=True)) #print(items) return items#按任意键逐条打印段子def getAll(parm): num=0 for x in parseHtml(parm): num += 1 print("第%d页第%d条:\n%s\n" %(parm,num,x)) #按回车键继续,python2中为raw_input if input("按回车键继续阅读下个段子..."): pass parm+=1 getAll(parm)if __name__ == '__main__': #从第一页开始 getAll(1)
阅读全文
0 0
- 【Python3.6】糗事百科爬虫
- [python3]糗事百科爬虫
- python3爬虫(二)实战- 爬糗事百科
- Python3爬虫糗事百科最新可用
- 糗事百科 爬虫 2017 10/1版本的糗事百科 python3.x
- Python3.4简单爬虫实现之抓取糗事百科段子
- Python3 定向爬虫之“抓取糗事百科图片”
- python3爬虫(2)--爬取糗事百科页面
- python3.5小爬虫—爬糗事百科
- [python3]爬虫实战一之爬取糗事百科段子
- Python3抓取糗事百科
- python3.0 糗事百科
- 糗事百科爬虫
- 糗事百科爬虫
- python爬虫糗事百科
- 糗事百科爬虫改进
- 糗事百科交互式爬虫
- 爬虫实战--糗事百科
- poj 2828 BuyTickets(线段树+插队问题)
- MySQL随机获取数据的方法,支持大数据量
- windows7 anaconda2 cpu caffe 环境搭建
- string类的实现
- 批量查杀后台driver进程及常见问题
- 【Python3.6】糗事百科爬虫
- centos新增用户以及赋权
- Evaluate the value of an arithmetic expression in Reverse Polish Notation.
- Sucuri 安全平台
- 9应用系统负载分析与磁盘容量预测
- 在linux下安装jdk,报错:gzip: stdin: not in gzip format
- liunx php版本升级
- 磁盘IOPS相关计算
- 作业5:输入一个一维数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。