python urllib爬取百度雲連線的例項程式碼

python urllib爬取百度雲連線的例項程式碼

翻看自己以前寫的程式,發現寫過一個爬取盤多多百度雲資源的東西,完全是當時想看變形金剛才自己寫的,而且當時第一次接觸python大概寫了有2天才搞出來這個程式,學習python語言,可以看得出來那時候的程式碼寫的真的low。雖然現在也不怎麼樣,哈哈,一直學習中,不做過多解釋,上圖上程式碼,因為變數的宣告是什麼我自己也忘了(手動傲嬌),連寫入檔案當時都不會哈哈哈哈哈哈哈哈,也不知道class中可以通過init初始化,唉學習python原來我學到了這麼多東西,感謝python


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('http://www.panduoduo.net/s/name/' search_text)
'''獲取百度雲地址'''
def getbaidu(adr):
for i in adr:
url = urllib.urlopen('http://www.panduoduo.net' 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('\<h1\sclass\=\"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)