Python爬虫
来源:互联网 发布:ubuntu虚拟机nat联网 编辑:程序博客网 时间:2024/06/09 20:47
参考慕课网:Python开发简单爬虫
1..架构:
2..URL管理器:
3..网页下载器:
urllib2(官方基础模块)、request(第三方包,更强大)
urllib2:
(1)简洁方法:
# -*- coding: utf-8 -*
import urllib2response=urllib2.urlopen("http://www.baidu.com")#########print response.getcode() #获取状态码,如果是200表示成功########cont=response.read()#读取下载好的内容(2)添加data、http、header:
data、http、header给urllib2。Request类,urllib2.urlopen(request)方法
import urllib2request=urllib2.Request(url)requset.add_data('a','1') #提交数据request.add_header('User-Agent','Mozilla/5.0') #提交http的header,伪装成Mozilla浏览器response=urllib2.urlopen(request)#########(3)添加特殊情景处理器:
import urllib2,cookielibcj=cookielib.CookieJar()#创建cookie容器opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))#创建一个openerurllib2.install_opener(opener)#为urllib2安装openerresponse=urllib2.urlopen("http://ww.baidu.com")###此时urllib2有了cookie处理的增强能力
4..网页解析器:
提取有价值数据
由html网页字符串提取出:有价值数据+新URL列表
网页解析器:①模糊匹配:正则表达式。②结构化解析(树):html.parser、Beautiful Soup、lxml
本课程:Beautiful Soup(支持正则表达式的匹配)官方文档
find_all(name,attrs,string),find(name,attrs,string)
# -*- coding: utf-8 -*from bs4 import BeautifulSoupsoup=BeautifulSoup(html_doc,'html.parser',from_encoding='utf-8')#html_doc:html文档字符串#'html.parser':html解析器 node=soup.find('a', href="/view/123.htm", string='Python')node2=soup.find('a', class_="title", string='Python')#class为python关键字,所以加_node.namenode['href']node.get_text()
5..实例:
分析目标的:URL格式、数据格式、网页编码……
6..
阅读全文
0 0
- python爬虫-->爬虫基础
- [爬虫] Python爬虫技巧
- Python爬虫
- python 爬虫
- python 爬虫
- python 爬虫
- python爬虫
- Python爬虫
- Python爬虫
- python 爬虫
- Python爬虫
- python爬虫
- python 爬虫
- python 爬虫
- python爬虫
- python爬虫
- python爬虫
- python 爬虫
- react-router 4.2.0<link>页面跳转-传参与不传参
- 压缩版JS的调试利器JavaScript Source Map基本介绍
- csp 2014_9_4 最优配餐
- Spark中groupByKey与reduceByKey算子之间的区别
- 蓝桥进制转换题集
- Python爬虫
- Log4j实例简单配置
- springboot下及其方便的使用缓存
- 算法-循环链表[约瑟夫问题]
- 内存初始化-----memset函数的使用
- 计算机语言发展史
- Java(8-1)
- Mysql02_索引
- CodeForces 888A Local Extrema