python urllib爬取百度云连接的实例代码的方法

来源:网络时间:2018-02-22 13:01:45

python urllib爬取百度云连接的实例代码的方法

?

1

2
3

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
word">from bs4 import BeautifulSoup
import urllib
import requests
import re
 
adr =[]
 
''''对搜素资源名字进行url编码'''
search_text =raw_input('请输入搜索资源名:')
search_text = search_text.decode('gbk')
search_text = search_text.encode('utf-8')
search_text = urllib.quote(search_text)
 
 
''''获取文件地址'''
home = urllib.urlopen('/s/name/'+search_text)
 
 
'''获取百度云地址'''
def getbaidu(adr):
  for i in adr:
    url = urllib.urlopen(''+i)
    bs = BeautifulSoup(url)
    bs1 = bs.select('.dbutton2')
    href = re.compile('http\%(\%|d|w|//|/|.)*')
    b = href.search(str(bs1))
    name = str(bs.select('.center')).decode('utf-8')
    text1 = re.compile('<h1sclass="center">[d|w|D|W]*</h1>')
    text2 = text1.search(name)
    rag1 = re.compile('>[d|w|D|W]*<')
    if text2:
      text3 = rag1.search(text2.group())
      if text3:
        print text3.group()
    if b:
      text = urllib.unquote(str(b.group())).decode('utf-8')
      print text
 
'''初始化'''
def init(adr):
  soup = BeautifulSoup(home)
  soup = soup.select('.row')
  pattern = re.compile('/r/d+')
  for i in soup:
    i = str(i)
    adress = pattern.search(i)
    adress = adress.group()
    adr.append(adress)
 
 
print 'running---------'   
init(adr)
getbaidu(adr)

文章内容来源于网络,不代表本站立场,若侵犯到您的权益,可联系我们删除。(本站为非盈利性质网站) 联系邮箱:rjfawu@163.com