1. 程式人生 > >提供一份爬蟲源碼,叫我雷鋒

提供一份爬蟲源碼,叫我雷鋒

agen 表達式 while art bsp head () 模塊 geo

#coding=utf-8
from Tkinter import * #GUI(圖像用戶界面)模塊
from ScrolledText import ScrolledText #文本滾動條
import urllib,requests #請求模塊
import re #正則表達式
import threading #多線程處理與控制
url_name = []#url+name
a = 1#頁碼
def get():
global a #全局變量
hd = {‘User-Agent‘:‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36‘}
url = ‘http://www.budejie.com/video/‘+str(a)
varl.set(‘已結獲取到第%s頁視頻‘%(a))
html = requests.get(url,headers=hd).text #獲取源碼
a += 1
url_content = re.compile(r‘<div class="j-r-list-c">.*?</div>.*?</div>‘,re.S)
url_contents = re.findall(url_content,html)
#print url_contents
for i in url_contents:
url_reg = r‘data-mp4="(.*?)">‘#匹配地址
url_items = re.findall(url_reg,i)
#print url_items #視頻列表
if url_items:#判斷地址列表是否存在
name_reg = re.compile(r‘<a href="/detail-.{8}?.html"(.*?)</\w>‘,re.S)
name_items = re.findall(name_reg,i)
#print name_items #名字列表
for i,k in zip(name_items,url_items):
url_name.append([i,k])
print i,k
return url_name
id = 1#視頻
def write():
global id
while id<10:
url_name = get()
for i in url_name:#名字+地址
#aa = i[0].decode(‘utf-8‘).encode(‘gbk‘)
urllib.urlretrieve(i[1],‘video\\%s.mp4‘%(a))
text.insert(END,str(id)+‘.‘+i[1]+‘\n‘+i[0]+‘\n‘)
url_name.pop(0)
id += 1
varl.set(‘視頻鏈接和名字抓取完畢,over‘)
def start():
th = threading.Thread(target=write)
th.start()#觸發

root = Tk()
root.title(‘爬取某視頻‘)
root.geometry(‘666x525‘)
text = ScrolledText(root,font=(‘微軟雅黑‘,10))
text.grid() #布局的方法 pack簡單
button = Button(root,text=‘開始爬取‘,font=(‘微軟雅黑‘,10),command=start)
button.grid()
varl = StringVar()
label = Label(root,font=(‘微軟雅黑‘,10),fg=‘red‘,textvariable = varl)
label.grid()
varl.set(‘已準備...‘)
root.mainloop()#發送創建窗口的指令

更多學習資料加群:595266089。

提供一份爬蟲源碼,叫我雷鋒