python抓页面基础知识
2016-10-11 17:30
260 查看
python抓页面基础知识
1.定义函数根据url获得页面
def getPageContent(self,url):
request = urllib2.Request(url)
response = urllib2.urlopen(request)
return response.read().decode('UTF-8')
2. 正则表达式匹配,re.S是dotall模式,意思是.表示所有字符的意思包括换行,()是分组的意思
pattern = re.compile('<div class="d_author">.*?<ul class="p_author">.*?'+
'<li class="d_name".*?<a.*?'+
'target="_blank">(.*?)</a>',re.S)
content = re.findall(pattern, pagecontent)
3.第二步中的content是数组,通过循环将数据写到文件,item是tuple类型使用[]使用,item[0]为第一列数据,item[1]为第二列,以此类推。
f = open('1.txt','a+')
for item in content:
f.writelines((str(item[0])+str(item[1])+item[2].encode('UTF-8'),f)
1.定义函数根据url获得页面
def getPageContent(self,url):
request = urllib2.Request(url)
response = urllib2.urlopen(request)
return response.read().decode('UTF-8')
2. 正则表达式匹配,re.S是dotall模式,意思是.表示所有字符的意思包括换行,()是分组的意思
pattern = re.compile('<div class="d_author">.*?<ul class="p_author">.*?'+
'<li class="d_name".*?<a.*?'+
'target="_blank">(.*?)</a>',re.S)
content = re.findall(pattern, pagecontent)
3.第二步中的content是数组,通过循环将数据写到文件,item是tuple类型使用[]使用,item[0]为第一列数据,item[1]为第二列,以此类推。
f = open('1.txt','a+')
for item in content:
f.writelines((str(item[0])+str(item[1])+item[2].encode('UTF-8'),f)
相关文章推荐
- python爬虫主要就是五个模块:爬虫启动入口模块,URL管理器存放已经爬虫的URL和待爬虫URL列表,html下载器,html解析器,html输出器 同时可以掌握到urllib2的使用、bs4(BeautifulSoup)页面解析器、re正则表达式、urlparse、python基础知识回顾(set集合操作)等相关内容。
- ASP页面基础知识
- 学学python(1)一些基础知识点
- Python基础知识(三)--基本的异常处理、算术运算符、输入/输出
- Python基础知识(八)--序列类型_元组
- Python基础知识(七)--字符串详解
- 基础知识之剖析ASP.NET页面
- Python基础知识(二)--控制流语句
- Python基础知识(一)
- Python基础知识(二)--控制流语句
- 适用于 PHP 开发人员的 Python 基础知识
- 【转】Python基础知识
- python学习笔记-第1章节 基础知识
- Python自学笔记(一)(Python基础知识)
- Python基础知识(五)--数据类型
- 适用于PHP开发人员的Python基础知识
- python基础知识
- Python基础知识汇总
- Python3基础知识(二)---- 元组、列表、字典、文件
- Python基础知识(一)