Python爬虫实战(六):爬取糗事百科段子
来源:互联网 发布:电子琴教学软件免费 编辑:程序博客网 时间:2024/05/28 17:04
直接上代码:
#coding=utf-8import requestsfrom bs4 import Tagfrom bs4 import BeautifulSoupdef getHtml(url): page = requests.get(url) html = page.text return htmldef getImg(html): get_text = Tag.get_text soup = BeautifulSoup(html, 'html.parser') info = soup.find_all('h2') user = (x.get_text() for x in info) #使用生成器代替list(map(get_text,info)) info = soup.find_all('span',class_="stats-vote") vote = (x.get_text() for x in info) #使用生成器代替list(map(get_text,info)) info = soup.find_all('div',class_="content") text = (x.get_text() for x in info) #使用生成器代替list(map(get_text,info)) for x in zip(user,text,vote): #zip函数同时遍历 Enter = input() if Enter == 'Q' or Enter == 'q': return -1 print("\n".join((s.strip() for s in x))) #使用生成器代替list(map(str.strip,x)) return 1 if __name__=='__main__': url = "https://www.qiushibaike.com/hot/" html,i = getHtml(url),2 print("开始读取数据,请按回车键...按Q键退出\n") while (getImg(html) == 1): print("\n第{}页\n".format(i)) url = ("https://www.qiushibaike.com/hot/page/%s/" % str(i)) html,i = getHtml(url),i+1
缺点:无法过滤掉既有文本,又有图片的内容。
待以后优化更新。
阅读全文
0 0
- Python爬虫实战(六):爬取糗事百科段子
- Python爬虫实战一之爬取糗事百科段子
- Python爬虫实战一之爬取糗事百科段子
- Python爬虫实战(1):爬取糗事百科段子
- Python爬虫实战(1):爬取糗事百科段子
- Python爬虫实战一之爬取糗事百科段子
- Python爬虫实战一之爬取糗事百科段子
- Python爬虫实战之爬取糗事百科段子
- Python爬虫实战一之爬取糗事百科段子
- Python爬虫实战一之爬取糗事百科段子
- python 爬虫 爬取糗事百科段子
- Python爬虫 爬取糗事百科段子
- Python爬虫爬取糗事百科段子
- Python爬虫 爬取糗事百科段子
- 第一次爬虫实战--爬取糗事百科段子
- [python3]爬虫实战一之爬取糗事百科段子
- Python 爬虫实战 - 糗事百科段子
- Python爬虫教程——实战一之爬取糗事百科段子
- 2017年随笔
- aiax defferred对象
- 成功劫持android的屏幕
- 解读MCE(Machine Check Exception)和Intel CSR寄存器(configuration Registers)的代码含义工具
- shiro(五)默认拦截器
- Python爬虫实战(六):爬取糗事百科段子
- 欢迎您在新浪博客安家
- 新的开始
- GitHub首次推送,出现的验证错误
- GitHub推送时区别使用http协议与ss…
- 关于RHEL6.5挂载光驱出现的错误
- 终于领悟到了,经济基础决定上层建…
- 更新yum源对应的配置文件
- libSVM在MATLAB中的初步使用