简单爬虫
2016-05-31 19:56
274 查看
import urllib.request
import re
########################################################
#
# fetch函数功能:抓取“陕西建筑招标网”的第一页“招标公告”页面,输出公告名字
#
# 参数baseUrl是要访问的网站地址
#
########################################################
def fetch(baseUrl):
# 第1步:模拟浏览器发送请求
page = urllib.request.urlopen(baseUrl)
data = page.read()
data = data.decode('utf-8')
# 第2步:页面返回后,利用正则表达式提取想要的内容
nameList=[]
nameList = re.compile(r'target="_blank" title="(.*?)"',re.DOTALL).findall(data)
# 第3步:返回在页面上析取的“标题名”
return nameList
####### 执行 ########
if __name__ =="__main__":
#要抓取的网页地址
url = "http://sh.uzai.com/lvyoucn/shandong-r-5113.html?utm_source=baidu&utm_medium=cpc&utm_term=%E5%B1%B1%E4%B8%9C%E6%97%85%E6%B8%B8%E7%BD%91&utm_content=%E5%B1%B1%E4%B8%9C-%E9%80%9A%E7%94%A8&utm_campaign=%E6%96%B0%E4%B8%8A%E6%B5%B7-%E5%9B%BD%E5%86%85-%E5%B1%B1%E4%B8%9C"
#存放到名字列表中
NameList = fetch(url)
# 输出 NameList
Length = len(NameList)
for i in range(0, Length):
print("标题名%d:%s\n"%(i+1, NameList[i]))
#将信息保存到doc 文档
f = open("E:/data.doc", "w+")
for i in range(0, len(NameList)):
f.write(NameList[i] + "\n")
f.close()
import re
########################################################
#
# fetch函数功能:抓取“陕西建筑招标网”的第一页“招标公告”页面,输出公告名字
#
# 参数baseUrl是要访问的网站地址
#
########################################################
def fetch(baseUrl):
# 第1步:模拟浏览器发送请求
page = urllib.request.urlopen(baseUrl)
data = page.read()
data = data.decode('utf-8')
# 第2步:页面返回后,利用正则表达式提取想要的内容
nameList=[]
nameList = re.compile(r'target="_blank" title="(.*?)"',re.DOTALL).findall(data)
# 第3步:返回在页面上析取的“标题名”
return nameList
####### 执行 ########
if __name__ =="__main__":
#要抓取的网页地址
url = "http://sh.uzai.com/lvyoucn/shandong-r-5113.html?utm_source=baidu&utm_medium=cpc&utm_term=%E5%B1%B1%E4%B8%9C%E6%97%85%E6%B8%B8%E7%BD%91&utm_content=%E5%B1%B1%E4%B8%9C-%E9%80%9A%E7%94%A8&utm_campaign=%E6%96%B0%E4%B8%8A%E6%B5%B7-%E5%9B%BD%E5%86%85-%E5%B1%B1%E4%B8%9C"
#存放到名字列表中
NameList = fetch(url)
# 输出 NameList
Length = len(NameList)
for i in range(0, Length):
print("标题名%d:%s\n"%(i+1, NameList[i]))
#将信息保存到doc 文档
f = open("E:/data.doc", "w+")
for i in range(0, len(NameList)):
f.write(NameList[i] + "\n")
f.close()
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- 爬虫笔记
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- ubuntu python django环境部署