【跟我学Python】第三章.场景三-- 使用Python解析抓取网页
使用Python访问网页主要有三种方式: urllib, urllib2, httplib
urllib比较简单,功能相对也比较弱,httplib简单强大,但好像不支持session
1. 最简单的页面访问
res=urllib2.urlopen(url)
print res.read()
2. 加上要get或post的数据
data={"name":"hank", "passwd":"hjz"}
urllib2.urlopen(url, urllib.urlencode(data))
3. 加上http头
header={"User-Agent": "Mozilla-Firefox5.0"}
urllib2.urlopen(url, urllib.urlencode(data), header)
更为复杂的操作:使用opener和handler协助操作其它web资源
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
4. 加上session
cj = cookielib.CookieJar()
cjhandler=urllib2.HTTPCookieProcessor(cj)
opener = urllib2.build_opener(cjhandler)
urllib2.install_opener(opener)
5. 加上Basic认证
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
top_level_url = "http://www.163.com/"
password_mgr.add_password(None, top_level_url, username, password)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
6. 使用代理
proxy_support = urllib2.ProxyHandler({"http":"http://1.2.3.4:3128/"})
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)
7. 设置超时
socket.setdefaulttimeout(5)
场景三:用Python写一个秒杀脚本,能够对淘宝特定页面进行解析
proxy_support = urllib2.ProxyHandler({"http":"http://proxy.****.com/"}) opener = urllib2.build_opener(proxy_support) urllib2.install_opener(opener) res = urllib2.urlopen('http://www.taobao.com/') print res.read() #将读取得到整个html页面 #接下来使用beautifulsoup扩展库对html中特定的div进行解析 from bs4 import * soup = BeautifulSoup(res.read( )) print(soup.find(id="div1")) #得到id=div1的div
相关推荐
从零开始学Python:循环结构.docx从零开始学Python:循环结构.docx从零开始学Python:循环结构.docx从零开始学Python:循环结构.docx从零开始学Python:循环结构.docx从零开始学Python:循环结构.docx从零开始学...
Python3编写实用脚本程序——从零学Python-掘金活动.zip Python3编写实用脚本程序——从零学Python-掘金活动.zip Python3编写实用脚本程序——从零学Python-掘金活动.zip Python3编写实用脚本程序——从零学Python-...
Python第三章.ppt
清华大学精品Python学习PPT课件-第3章 Python流程控制.pptx 清华大学精品Python学习PPT课件-第4章 Python组合数据类型.pptx 清华大学精品Python学习PPT课件-第5章 Python正则表达式.pptx 清华大学精品Python学习PPT...
1_Python第三章.zip
2010-09-07 12:43 220,160 boost_python-vc90-mt-1_44.dll 2010-09-07 12:43 189,272 boost_python-vc90-mt-1_44.lib ...2010-09-07 11:37 3,396,608 python27_d.dll 2010-09-07 11:37 236,300 python27_d.lib
AW.Learn.Python.3.the.Hard.Way无水印版,笨办法学python,笨方法学python 3,不是第三版,是python3
清华大学精品Python学习PPT课件-第3章 Python流程控制.pptx 清华大学精品Python学习PPT课件-第4章 Python组合数据类型.pptx 清华大学精品Python学习PPT课件-第5章 Python正则表达式.pptx 清华大学精品Python学习PPT...
清华大学精品Python学习PPT课件-第3章 Python流程控制.pptx 清华大学精品Python学习PPT课件-第4章 Python组合数据类型.pptx 清华大学精品Python学习PPT课件-第5章 Python正则表达式.pptx 清华大学精品Python学习PPT...
清华大学精品Python学习PPT课件-第3章 Python流程控制.pptx 清华大学精品Python学习PPT课件-第4章 Python组合数据类型.pptx 清华大学精品Python学习PPT课件-第5章 Python正则表达式.pptx 清华大学精品Python学习PPT...
清华大学精品Python学习PPT课件-第3章 Python流程控制.pptx 清华大学精品Python学习PPT课件-第4章 Python组合数据类型.pptx 清华大学精品Python学习PPT课件-第5章 Python正则表达式.pptx 清华大学精品Python学习PPT...
python27_d.lib python27的debug版本的动态库文件
Python从入门到精通 第3章 运算符与表达式.ppt Python从入门到精通 第4章 流程控制语句.ppt Python从入门到精通 第5章 列表与元组.ppt Python从入门到精通 第6章 字典与集合.ppt Python从入门到精通 第7章 字符串....
Python爬虫详细解析.doc Python爬虫详细解析.doc Python爬虫详细解析.doc Python爬虫详细解析.doc Python爬虫详细解析.doc Python爬虫详细解析.doc Python爬虫详细解析.doc Python爬虫详细解析.doc
第3章 Python流程控制.pptx 第4章 Python组合数据类型.pptx 第5章 Python正则表达式.pptx 第6章 Python函数.pptx 第7章 Python模块.pptx 第8章 Python类和对象.pptx 第9章 Python异常.pptx 第10章 Python文件操作....
清华大学精品Python学习PPT课件-第3章 Python流程控制.pptx 清华大学精品Python学习PPT课件-第4章 Python组合数据类型.pptx 清华大学精品Python学习PPT课件-第5章 Python正则表达式.pptx 清华大学精品Python学习PPT...
清华大学精品Python学习PPT课件-第3章 Python流程控制.pptx 清华大学精品Python学习PPT课件-第4章 Python组合数据类型.pptx 清华大学精品Python学习PPT课件-第5章 Python正则表达式.pptx 清华大学精品Python学习PPT...
清华大学精品Python学习PPT课件-第3章 Python流程控制.pptx 清华大学精品Python学习PPT课件-第4章 Python组合数据类型.pptx 清华大学精品Python学习PPT课件-第5章 Python正则表达式.pptx 清华大学精品Python学习PPT...
python网页文本爬虫.pdfpython网页文本爬虫.pdfpython网页文本爬虫.pdfpython网页文本爬虫.pdfpython网页文本爬虫.pdfpython网页文本爬虫.pdfpython网页文本爬虫.pdfpython网页文本爬虫.pdf
第3章 Python流程控制.pptx 第4章 Python组合数据类型.pptx 第5章 Python正则表达式.pptx 第6章 Python函数.pptx 第7章 Python模块.pptx 第8章 Python类和对象.pptx 第9章 Python异常.pptx 第10章 Python文件操作....