您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Python多線程爬蟲(chóng)的使用方法是什么”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
Python 3.6
Pycharm
wkhtmltopdf
re
requests
concurrent.futures
安裝Python并添加到環(huán)境變量,pip安裝需要的相關(guān)模塊即可。
現(xiàn)在聊天誰(shuí)還不發(fā)幾個(gè)表情包?聊天時(shí),表情包是我們重要的工具,更是拉進(jìn)小伙伴們距離的好幫手,當(dāng)聊天陷入尷尬境地時(shí),隨手一張表情包,讓尷尬化為無(wú)形
如圖所示斗圖網(wǎng)上面的圖片數(shù)據(jù)都包含在 a 標(biāo)簽當(dāng)中,可以嘗試直接請(qǐng)求這個(gè)網(wǎng)頁(yè),查看response 返回的數(shù)據(jù)當(dāng)中是否也含有 圖片地址。
import requests def get_response(html_url): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36' } response = requests.get(url=html_url, headers=headers) return response def main(html_url): response = get_response(html_url) print(response.text) if __name__ == '__main__': url = 'https://www.doutula.com/photo/list/' main(url)
在輸出結(jié)果中 ctrl + F 進(jìn)行搜索。
這里有一個(gè)點(diǎn)想要注意一下,我用python請(qǐng)求網(wǎng)頁(yè)所給我們返回的結(jié)果當(dāng)中,包含圖片url地址是:
data-original="圖片url"
data-backup="圖片url"
如果想要提取url地址的話,可以用parsel 解析庫(kù),或者 re 正則表達(dá)式。之前都是使用的parsel,本篇文章就用 正則表達(dá)式吧。
urls = re.findall('data-original="(.*?)"', response.text)
import requests import re def get_response(html_url): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36' } response = requests.get(url=html_url, headers=headers) return response def save(image_url, image_name): image_content = get_response(image_url).content filename = 'images\\' + image_name with open(filename, mode='wb') as f: f.write(image_content) print(image_name) def main(html_url): response = get_response(html_url) urls = re.findall('data-original="(.*?)"', response.text) for link in urls: image_name = link.split('/')[-1] save(link, image_name) if __name__ == '__main__': url = 'https://www.doutula.com/photo/list/' main(url)
3631頁(yè)的數(shù)據(jù),什么表情都有,嘿嘿嘿
import requests import re import concurrent.futures def get_response(html_url): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36' } response = requests.get(url=html_url, headers=headers) return response def save(image_url, image_name): image_content = get_response(image_url).content filename = 'images\\' + image_name with open(filename, mode='wb') as f: f.write(image_content) print(image_name) def main(html_url): response = get_response(html_url) urls = re.findall('data-original="(.*?)"', response.text) for link in urls: image_name = link.split('/')[-1] save(link, image_name) if __name__ == '__main__': # ThreadPoolExecutor 線程池的對(duì)象 # max_workers 最大任務(wù)數(shù) executor = concurrent.futures.ThreadPoolExecutor(max_workers=3) for page in range(1, 3632): url = f'https://www.doutula.com/photo/list/?page={page}' # submit 往線程池里面添加任務(wù) executor.submit(main, url) executor.shutdown()
“Python多線程爬蟲(chóng)的使用方法是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。