Python爬虫实例——基于urlib、urlib和re实现
来源:互联网 发布:sem跟seo的比较区别 编辑:程序博客网 时间:2024/06/02 07:14
爬取的目标网页:http://www.qianlima.com/zb/area_305/
这是一个招投标网站,我们使用python脚本爬取红框中的信息,包括链接网址、链接名称、时间等三项内容。
使用到的Python库:urlib、urlib2、re(正则表达式库)
代码如下:
# -*- coding:utf-8 -*-import urllibimport urllib2import reurl = 'http://www.qianlima.com/zb/area_305/'user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'headers = { 'User-Agent' : user_agent}try:request = urllib2.Request(url,headers = headers)response = urllib2.urlopen(request)content = response.read().decode('gbk')pattern = re.compile(u'<span class="shixian_biao">招标公告</span>(.*?)<span class="shixian_biao">招标预告</span>',re.S)items = re.findall(pattern,content)item = items[0]pattern = re.compile('<dt><a href="(.*?)" target="_blank">(.*?)</a></dt><dd>(.*?)</dd>',re.S)items = re.findall(pattern,item)for m in items:print '%s\t%s\t%s' % (m[0],m[1],m[2])except urllib2.URLError, e:if hasattr(e,"code"):print e.codeif hasattr(e,"reason"):print e.reason这里使用到了两次正则表达式匹配
运行结果如下
阅读全文
1 0
- Python爬虫实例——基于urlib、urlib和re实现
- Python urlib模块学习
- python-urlib模块
- Python urlib vs urlib2
- Python学习之常用内置模块:urlib
- Python爬虫实例——基于BeautifulSoup和requests实现
- Python使用urlib添加头部实现https请求 同时使用PyExecJS-1.4.0触发JS
- Python学习笔记-1、Python3的urlib.request和bs4.BeautifulSoup使用
- python3 爬虫入门(一)urlib库基本使用
- urlib.unquote函数
- Python 使用urlib下载美空网页的模特照片
- urlib在python3的使用
- Python网络爬虫——Re库
- Python爬虫实例(4)-用urllib、re和正则表达式爬取网页图片
- Python爬虫学习三——re库
- 基于Python、PyQuery实现的一个网络爬虫实例
- Python爬虫正则表达式和re模块系列之七
- python——爬虫学习——正则表达式与Re库-(4)
- Tyvj P1013 找啊找啊找GF
- 十项全能革新网站开发技术-Zoomla!逐浪CMS2 x3.9.1发布
- java连接数据库
- 【代码笔记】iOS-iOS图片的原生(Graphics)
- linux服务之Apache
- Python爬虫实例——基于urlib、urlib和re实现
- 在正常系统如windows/MacOS/Linux等体验假勒索病毒WamaCry(永恒之蓝)
- 深度学习框架- 设计思维
- android 读取assets下的*.sql并执行sql语句
- Tesseract3.04 和opencv3.1在条形码识别中的应用
- WinSCP 下修改用户权限到root方法
- HiveSQL 常用日期
- 深入理解C# 静态类与非静态类、静态成员的区别
- centos 6上登录oracle数据前报错