python爬取糗百内容
#-*- coding: utf-8 -*-
import urllib
import urllib2
import re #页面为1
page=1
url='http://www.qiushibaike.com/hot/page/'+str(page) #需要header验证
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = { 'User-Agent' : user_agent }
try:
#获取地址
request=urllib2.Request(url,headers=headers)
#打开连接
response=urllib2.urlopen(request)
#输出读取内容
#print response.read() content=response.read().decode('utf-8') # 去奇趣百科找不带图片的段子结构,匹配正则,糗百的标签会不定时改变,正则可能要重新匹配
pattern = re.compile(
'<div.*?author.*?users.*?<h2>(.*?)</h2>.*?content.*?<span>(.*?)</span>.*?vote.*?number">(.*?)</i>.*?comments.*?number">(.*?)</i>',
re.S)
#<div.*?author.*?users.*?<h2>(.*?)</h2>.*?content.*?<span>(.*?)</span>.*?<a.*?img.*?>(.*?)</a>
# 组 作者,内容,点赞,评论
items=re.findall(pattern,content) for item in items: print item[0],item[1],item[3]
except urllib2.URLError,e:
if hasattr(e,'code'):
print e.code
if hasattr(e,'reason'):
print e.reason
python爬取糗百内容的更多相关文章
- python爬取糗百第一页的笑话
自学python网络爬虫,发现request比urllib还是要好用一些,因此利用request和BeautifulSoup来实现糗百的首页笑话的抓取.BeautifulSoup通过find和find ...
- python爬取页面内容
from selenium import webdriverimport xlwt driver = webdriver.Chrome(r'D:\chromedriver.exe')driver.ma ...
- Python 爬取页面内容
import urllib.request import requests from bs4 import BeautifulSoup url = "http://www.stats.gov ...
- Python 爬虫入门(一)——爬取糗百
爬取糗百内容 GitHub 代码地址https://github.com/injetlee/Python/blob/master/qiubai_crawer.py 微信公众号:[智能制造专栏],欢迎关 ...
- python爬取网站数据
开学前接了一个任务,内容是从网上爬取特定属性的数据.正好之前学了python,练练手. 编码问题 因为涉及到中文,所以必然地涉及到了编码的问题,这一次借这个机会算是彻底搞清楚了. 问题要从文字的编码讲 ...
- Python:爬取乌云厂商列表,使用BeautifulSoup解析
在SSS论坛看到有人写的Python爬取乌云厂商,想练一下手,就照着重新写了一遍 原帖:http://bbs.sssie.com/thread-965-1-1.html #coding:utf- im ...
- python爬取百度搜索结果ur汇总
写了两篇之后,我觉得关于爬虫,重点还是分析过程 分析些什么呢: 1)首先明确自己要爬取的目标 比如这次我们需要爬取的是使用百度搜索之后所有出来的url结果 2)分析手动进行的获取目标的过程,以便以程序 ...
- [实战演练]python3使用requests模块爬取页面内容
本文摘要: 1.安装pip 2.安装requests模块 3.安装beautifulsoup4 4.requests模块浅析 + 发送请求 + 传递URL参数 + 响应内容 + 获取网页编码 + 获取 ...
- Python爬取腾讯新闻首页所有新闻及评论
前言 这篇博客写的是实现的一个爬取腾讯新闻首页所有的新闻及其所有评论的爬虫.选用Python的Scrapy框架.这篇文章主要讨论使用Chrome浏览器的开发者工具获取新闻及评论的来源地址. Chrom ...
随机推荐
- 八皇后问题动态演示_Qt5实现
//核心代码如下 //Queen--放置皇后 #include "queue.h" queue::queue() { *; ; this->board = new bool[ ...
- 013、Dockerfile构建镜像(2019-01-02 周三)
参考https://www.cnblogs.com/CloudMan6/p/6830067.html Dockerfile构建镜像过程分析 root@docker-lab:~/111# ls ...
- centos7 memcached+magent+keepalived集群
111,222均部署keepalived,magent,memcached keepalived 111为主机,222为备机 其中,111上magent以本地memcache为主,222为备用 222 ...
- Oracle语句优先级
SQL> SELECT SAL SALARY FROM EMP WHERE SALARY<2500;Warning: connection was lost and re-establis ...
- 02、@PropertySource指定配置文件的属性映射到JavaBean属性
零.@PropertySource 功能类似于 <context:property-placeholder location="classpath*:/config/load.prop ...
- C# 批量修改文件名
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 字体选择框QFontComboBox
self.combobox_2 = QFontComboBox(self) # 实例化字体列表框 combobox.currentFont() 返回字体选择框中当前的字体 self.combobo ...
- Git查看单个文件修改历史
1 命令 git log --pretty=oneline 文件名 ➜ admin git:(feature/v1.5.0_20181202_group) git log --pretty=onel ...
- Java9都快发布了,Java8的十大新特性你了解多少呢?
Java 9预计将于今年9月份发布,这是否会是一次里程碑式的版本,我们拭目以待.今天,我们先来复习一下2014年发布的Java 8的十大新特性. Java 8可谓是自Java 5以来最具革命性的版本了 ...
- 同步sync 异步async
线程中 同步任务是串行队列,也就是按顺序执行. 同步任务:不会开辟新的线程,它是在当前线程执行的. dispatch 调度 GCD里面的函数都是以dispatch开头的. 同步任务 步骤: 1. ...