利用Python多執行緒爬了5000多部最新電影下載鏈接,廢話不多說~
讓我們愉快地開始吧~
開發工具
Python版本:3.6.4
相關模塊:
requests模塊;
re模塊;
csv模塊;
以及一些Python自帶的模塊,
環境搭建
安裝Python并添加到環境變數,pip安裝需要的相關模塊即可,
很多人學習蟒蛇,不知道從何學起, 很多人學習python,掌握了基本語法之后,不知道在哪里尋找案例上手, 很多已經做了案例的人,卻不知道如何去學習更多高深的知識, 那么針對這三類人,我給大家提供一個好的學習平臺,免費獲取視頻教程,電子書,以及課程的源代碼! QQ群:101677771 歡迎加入,一起討論一起學習
bs = BeautifulSoup(html, "html.parser")
b = bs.findAll(class_="co_content8")
b = b[0].findAll(class_="ulink")
拿到鏈接之后,接下來就是繼續訪問這些鏈接,然后拿到電影的下載鏈接
bs1 = BeautifulSoup(html1, "html.parser")
b1 = bs1.find("tbody").find_next("td").find_next("a")
download_url = b1.get("href")
但是這里還是有很多的小細節,例如我們需要拿到電影的總頁數,其次這么多的頁面,一個執行緒不知道要跑到什么時候,所以我們首先先拿到總頁碼,然后用多執行緒來進行任務的分配
我們首先先拿到總頁碼,然后用多執行緒來進行任務的分配
總頁數其實我們用re正則來獲取
def get_total_page(url):
r = requests.get(url=url,headers=headers)
r.encoding = 'gb2312'
pattern = re.compile(r'(?<=頁/)\d+')
t = pattern.findall(r.text)
return int(t[0])
爬取的內容存取到csv,也可以寫個函式來存取
def wirte_into_csv(name,down_url):
f = open('最新電影.csv', 'a+', encoding='utf-8')
csv_writer = csv.writer(f)
csv_writer.writerow([name,down_url])
f.close()
本文原始碼滴我666
開啟4個行程來下載鏈接
total_page = get_total_page("https://www.ygdy8.com/html/gndy/oumei/list_7_1.html")
total_page = int(total_page/25+1)
end = int(total_page/4)
try:
_thread.start_new_thread(run, (1, end))
_thread.start_new_thread(run, (end+1, end*2))
_thread.start_new_thread(run, (end*2 + 1, end * 3))
_thread.start_new_thread(run, (end*3 + 1, end * 4))
except:
print("Error: 無法啟動執行緒")
while(1):
pass
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/398413.html
標籤:其他
