百度云爬虫_python

分享一个小技巧,对于一些有反爬机制网站的爬取,可以从wap端入手。比如百度云的资料。

具体代码如下:

#coding:utf-8
'''
Created on 2016年2月27日
@author: Jay
'''
import urllib
import urllib2
import re
import time

uk=2214641459
url0='http://yun.baidu.com/share'
url1='http://yun.baidu.com/wap/share/home?third=0&uk='+str(uk)+'&start='
'''
proxy = {'http':'27.24.158.155:84'}
proxy_support = urllib2.ProxyHandler(proxy)
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)

exemples:
pages:
url='http://yun.baidu.com/wap/share/home?third=0&uk=2214641459&start='
links:
url='http://yun.baidu.com/wap/link?uk=2214641459&shareid=640316896&third=0'
'''
def getResponse(url):
    headers = {"User-Agent":"Mozilla/5.0 (Linux; Android 4.4.2; Nexus 4 Build/KOT49H)"}
    request=urllib2.Request(url,headers=headers)
    res=urllib2.urlopen(request).read()
    return res

def getNames(response):
    pattern = re.compile('<h3>(.+?)</h3>')
    names=re.findall(pattern, response)
    return names

def getLinks(response):
    response=re.sub('amp;','',response)
    pattern = re.compile('"list-item"\shref="/wap(.+?)"')
    links=re.findall(pattern, response)
    return [url0+i for i in links]

def getTitle(response):
    pattern = re.compile('<title>(.+?)</title>')
    title=re.search(pattern, response)
    return title

def getTotalNum(response):
    pattern = re.compile('totalCount:"(\d+)"')
    num=re.search(pattern, response)
    return num.group(1)

res=getResponse(url1)
#print res
f=open(r'/Volumes/File/Baidu_Share.txt','w')
num=getTotalNum(res)
#print num
MAX=(int(num)/20+1)*20
urls=[url1+str(i) for i in range(0,MAX,20)]

for url in urls:
    #time.sleep(2)
    res=getResponse(url)
    names=getNames(res)
    print names
    links=getLinks(res)
    com=zip(names,links)
    for c in com:
        f.write(c[0]+':'+'\n')
        f.write(c[1]+'\n'+'\n')
f.close()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值