用python看段子
来源:互联网 发布:mysql fetch array 编辑:程序博客网 时间:2024/05/17 08:45
周末两天时间看了一下python的语法。看完之后,总得拿个东西练练手,于是就想到了去抓取糗事百科的段子,代码如下。
#!/usr/bin/env python3# -*- coding: utf-8 -*-__author__ = 'Oliver Hu'from urllib import requestfrom bs4 import BeautifulSoupimport osurl = 'https://www.qiushibaike.com/text/'user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'class Jokes: def __init__(self): self.pageNo = 1 self.jokes = [] def getPage(self, pageNo): req = request.Request(url + 'page/' + pageNo) req.add_header('User-Agent', user_agent) with request.urlopen(req) as html: html = html.read().decode('utf-8') soup = BeautifulSoup(html, 'html.parser') contents = soup.select('.content') count = len(contents) for index, text in enumerate(contents): text = text.get_text().strip() if text.find('查看全文') == -1: print(text) if count - 1 > index: command = input('\n按回车查看下一条...q退出\n') if command.lower() == 'q': break else: os.system('cls') continue else: command = input('\n按回车查看下一条...q退出\n') if command.lower() == 'q': break else: os.system('cls') self.pageNo = self.pageNo + 1 self.getPage(str(self.pageNo)) else: print() def run(self): self.getPage(str(self.pageNo))print('''***********************糗事百科小爬虫描述:用另类的方式看段子作者:Oliver Hu日期:2017-09-17语言:python 3.6.2***********************''')joke = Jokes()joke.run()
运行结果如下图
代码比较简单,就不多说了,无聊的时候可以玩一玩。
阅读全文
0 0
- 用python看段子
- python 爬虫糗事段子中的段子
- 使用Python爬取糗百段子
- 用python 爬糗事百科的段子-1
- Python抓取段子的爬虫
- python实现爬虫抓取段子
- python爬虫爬取段子
- 程序猿看段子,越看越心碎!
- 段子
- 段子
- 段子
- 段子
- 段子
- 段子
- python抓取糗事百科的段子
- 《python爬虫实战》:爬糗百上的段子
- Python实现抓取糗事百科的段子
- python爬虫之糗百段子
- PullToRefreshListView
- TCP疑问记录
- javaEE面试题集锦(持续更新)
- 使用Jedis操作redis
- Vue2 局部拓展extends 先于局部mixins执行
- 用python看段子
- SpringMVC和Servlet区别对比
- JAVA-常用包 与 import
- fiddler-实现https抓包
- Java 集合源码解析(2):ListIterator
- java 复制一个类中的属性给另一个具有相同属性的类
- 论文笔记:Spatial Transformer Networks(空间变换网络)
- 图片无限轮播
- 对于volist标签出现最后一个显示为空