python 爬虫学习笔记(1)
2017-04-07 10:08
405 查看
目标:爬取糗事百科的段子代码:
# -*- coding: utf-8 -*- __author__ = 'beauty'
import sys type = sys.getfilesystemencoding() #为了防止出现乱码
import urllib2import repage = 1url = 'http://www.qiushibaike.com/hot/page/' + str(page)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)content = response.read().decode('utf-8')# print content.encode(type)pattern = re.compile('<div class="author clearfix">.*?href.*?<img src.*?title=.*?<h2>(.*?)</h2>.*?<div class="content">(.*?)</div>.*?<i class="number">(.*?)</i>',re.S)items = re.findall(pattern,content)# print itemsfor item in items:print item[0].encode(type),item[1].encode(type),item[2].encode(type)except urllib2.URLError, e:if hasattr(e,"code"):print e.codeif hasattr(e,"reason"):print e.reason在pycharm中的运行结果:
相关文章推荐
- Python3.x学习笔记[1]:2种简单爬虫获取京东价格
- Python爬虫框架Scrapy 学习笔记 1 ----- 环境搭建
- python爬虫学习笔记(一)
- python2.7爬虫学习笔记(一)---Urllib库的使用
- Python学习笔记之网络爬虫
- Python学习笔记(四) -- 简单的爬虫
- 【Python爬虫学习笔记(1)】urllib2库相关知识点总结
- python学习笔记之爬虫之爬取百度贴吧某一帖子
- 【Python爬虫学习笔记(2)】正则表达式(re模块)相关知识点总结
- Python爬虫框架Scrapy 学习笔记 4 ------- 第二个Scrapy项目
- python爬虫框架scrapy学习笔记
- python爬虫框架scrapy学习笔记
- Python爬虫框架Scrapy 学习笔记 8----Spider
- python学习笔记:"爬虫+有道词典"实现一个简单的英译汉程序
- Python爬虫框架Scrapy 学习笔记 9 ----selenium
- Python爬虫框架Scrapy 学习笔记 10.2 -------【实战】 抓取天猫某网店所有宝贝详情
- Python爬虫框架Scrapy 学习笔记 10.3 -------【实战】 抓取天猫某网店所有宝贝详情
- 【python学习笔记】网络爬虫的完整源代码
- python 爬虫学习笔记2
- python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例