由于工作需要,最近刚刚开始自学python爬虫,最终的目标是能够随心所欲的爬取各种能看到的网页内容。这同时也是在优快云上第一篇博客,作为一个崭新的开始吧。
一般的网页html源码获取很简单 ,以深圳公积金快速查询页面为例,以下两段代码都可以通过urllib2很方便的把页面源码爬下来
import urllib2
response = urllib2.urlopen('http://www.szzfgjj.com/zhcx/index.htm')
html = response.read()
print html
import urllib2
req = urllib2.Request('http://www.szzfgjj.com/zhcx/index.htm')
response = urllib2.urlopen(req)
the_page = response.read()
print the_page
以下实现的功能是,下载验证码图片文件并显示。提示:需要下载安装PIL(http://www.pythonware.com/products/pil/)
import urllib,Image
imgurl='http://www.szzfgjj.com/code.jsp'
mypath='E:\MyPython\gjjcode.jpg'
saveimg=urllib.urlretrieve(imgurl,mypath)
im = Image.open('E:\MyPython\gjjcode.jpg')
im.show()
后面计划通过人工识别验证码,和其它参数一起自动登录该页面,并获取得到的信息