python页面爬虫_python网页爬虫

1. 静态页面爬取

这类最简单啦,右键->查看页面源码时,想下载的信息都能够显示在这里,这时只需要直接down页面源码,代码如下:

#Simple open web

importurllib2print urllib2.urlopen('http://stockrt.github.com').read()#With password?

importurllib

opener=urllib.FancyURLopener()print opener.open('http://user:password@stockrt.github.com').read()

2. 滑动鼠标动态加载内容

有些页面在打开时不会完全显示,而是通过滑动鼠标动态加载。对于这类页面的爬虫,需要找到触发动态加载的url,通常方法为:右键->审查元素->Network

寻找滑动鼠标时触发的事件,分析每次滑动鼠标时url中变化的参数,在代码中拼接出对应的url即可。

3. 使用 mechanize 模拟浏览器访问网页

有时会发现上述方法不灵,即down的东西与页面内容不一致,会发现内容少了很多,这时就需要浏览器伪装,模拟浏览器动作,在命令行或者python脚本中实例化一个浏览器。代码

模拟浏览器:

importmechanizeimportcookielib#Browser

br =mechanize.Browser()#Cookie Jar

cj =cookielib.LWPCookieJar()

br.set_cookiejar(cj)#Browser options

br.set_handle_equiv(True)

br.set_handle_gzip(True)

br.set_handle_redirect(True)

br.set_handle_referer(True)

br.set_handle_robots(False)#Follows refresh 0 but not hangs on refresh > 0

br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)#Want debugging messages?#br.set_debug_http(True)#br.set_debug_redirects(True)#br.set_debug_responses(True)#User-Agent (this is cheating, ok?)

br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

现在你得到了一个浏览器的示例,br对象。使用这个对象,便可以打开一个页面,使用类似如下的代码:

#Open some site, let's pick a random one, the first that pops in mind:

r = br.open('http://google.com')

html=r.read()#Show the source

printhtml#or

printbr.response().read()#Show the html title

printbr.title()#Show the response headers

printr.info()#or

printbr.response().info()#Show the available forms

for f inbr.forms():printf#Select the first (index zero) form

br.select_form(nr=0)#Let's search

br.form['q']='weekend codes'br.submit()printbr.response().read()#Looking at some results in link format

for l in br.links(url_regex='stockrt'):print l

如果你访问的网站需要验证(http basic auth),那么:

#If the protected site didn't receive the authentication data you would#end up with a 410 error in your face

br.add_password('http://safe-site.domain', 'username', 'password')

br.open('http://safe-site.domain')

由于之前使用了Cookie Jar,你不需要管理网站的登录session。也就是不需要管理需要POST一个用户名和密码的情况。

通常这种情况,网站会请求你的浏览器去存储一个session cookie除非你重复登陆,

而导致你的cookie中含有这个字段。所有这些事情,存储和重发这个session cookie已经被Cookie Jar搞定了,爽吧。

同时,你可以管理你的浏览器历史:

#Testing presence of link (if the link is not found you would have to#handle a LinkNotFoundError exception)

br.find_link(text='Weekend codes')#Actually clicking the link

req = br.click_link(text='Weekend codes')

br.open(req)printbr.response().read()printbr.geturl()#Back

br.back()printbr.response().read()print br.geturl()

下载一个文件:

#Download

f = br.retrieve('http://www.google.com.br/intl/pt-BR_br/images/logo.gif')[0]printf

fh= open(f)

为http设置代理

#Proxy and user/password

br.set_proxies({"http": "joe:password@myproxy.example.com:3128"})#Proxy

br.set_proxies({"http": "myproxy.example.com:3128"})#Proxy password

br.add_proxy_password("joe", "password")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值