python 多線程爬取壁紙網(wǎng)站的示例
基本開發(fā)環(huán)境
· Python 3.6
· Pycharm
需要導(dǎo)入的庫
目標(biāo)網(wǎng)頁分析
網(wǎng)站是靜態(tài)網(wǎng)站,沒有加密,可以直接爬取
整體思路:
1、先在列表頁面獲取每張壁紙的詳情頁地址
2、在壁紙?jiān)斍轫撁娅@取壁紙真實(shí)高清url地址
3、保存地址
代碼實(shí)現(xiàn)
模擬瀏覽器請請求網(wǎng)頁,獲取網(wǎng)頁數(shù)據(jù)
這里只選擇爬取前10頁的數(shù)據(jù)
代碼如下
import threading import parsel import requests def get_html(html_url): ''' 獲取網(wǎng)頁源代碼 :param html_url: 網(wǎng)頁url :return: ''' response = requests.get(url=html_url, headers=headers) return response def get_par(html_data): ''' 把 response.text 轉(zhuǎn)換成 selector 對象 解析提取數(shù)據(jù) :param html_data: response.text :return: selector 對象 ''' selector = parsel.Selector(html_data) return selector def download(img_url, title): ''' 保存數(shù)據(jù) :param img_url: 圖片地址 :param title: 圖片標(biāo)題 :return: ''' content = get_html(img_url).content path = '壁紙\\' + title + '.jpg' with open(path, mode='wb') as f: f.write(content) print('正在保存', title) def main(url): ''' 主函數(shù) :param url: 列表頁面 url :return: ''' html_data = get_html(url).text selector = get_par(html_data) lis = selector.css('.wb_listbox div dl dd a::attr(href)').getall() for li in lis: img_data = get_html(li).text img_selector = get_par(img_data) img_url = img_selector.css('.wb_showpic_main img::attr(src)').get() title = img_selector.css('.wb_pictitle::text').get().strip() download(img_url, title) end_time = time.time() - s_time print(end_time) if __name__ == '__main__': for page in range(1, 11): url = 'http://www.deskbizhi.com/min/list-{}.html'.format(page) main_thread = threading.Thread(target=main, args=(url,)) main_thread.start()
以上就是python 多線程爬取壁紙網(wǎng)站的示例的詳細(xì)內(nèi)容,更多關(guān)于python 爬取壁紙網(wǎng)站的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python中線程的MQ消息隊(duì)列實(shí)現(xiàn)以及消息隊(duì)列的優(yōu)點(diǎn)解析
消息隊(duì)列(MQ,Message Queue)在消息數(shù)據(jù)傳輸中的保存作用為數(shù)據(jù)通信提供了保障和實(shí)時(shí)處理上的便利,這里我們就來看一下Python中線程的MQ消息隊(duì)列實(shí)現(xiàn)以及消息隊(duì)列的優(yōu)點(diǎn)解析2016-06-06LyScript實(shí)現(xiàn)Hook隱藏調(diào)試器的方法詳解
LyScript?插件集成的內(nèi)置API函數(shù)可靈活的實(shí)現(xiàn)繞過各類反調(diào)試保護(hù)機(jī)制。本文將運(yùn)用LyScript實(shí)現(xiàn)繞過大多數(shù)通用調(diào)試機(jī)制,實(shí)現(xiàn)隱藏調(diào)試器的目的,需要的可以參考一下2022-09-09基于Python __dict__與dir()的區(qū)別詳解
下面小編就為大家?guī)硪黄赑ython __dict__與dir()的區(qū)別詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10基于Keras的格式化輸出Loss實(shí)現(xiàn)方式
這篇文章主要介紹了基于Keras的格式化輸出Loss實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python中文分詞工具之結(jié)巴分詞用法實(shí)例總結(jié)【經(jīng)典案例】
這篇文章主要介紹了Python中文分詞工具之結(jié)巴分詞用法,結(jié)合實(shí)例形式總結(jié)分析了Python針對中文文件的讀取與分詞操作過程中遇到的問題與解決方法,需要的朋友可以參考下2017-04-04python3?cookbook解壓可迭代對象賦值給多個(gè)變量的問題及解決方案
這篇文章主要介紹了python3?cookbook-解壓可迭代對象賦值給多個(gè)變量,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-01-01Python實(shí)現(xiàn)的讀寫json文件功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)的讀寫json文件功能,結(jié)合實(shí)例形式分析了Python針對json文件進(jìn)行讀寫的常見操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-06-06