国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python 多線程爬取壁紙網(wǎng)站的示例

瀏覽:161日期:2022-06-27 14:15:52
基本開發(fā)環(huán)境

· Python 3.6

· Pycharm

需要導(dǎo)入的庫(kù)

python 多線程爬取壁紙網(wǎng)站的示例

目標(biāo)網(wǎng)頁(yè)分析

python 多線程爬取壁紙網(wǎng)站的示例

網(wǎng)站是靜態(tài)網(wǎng)站,沒有加密,可以直接爬取

python 多線程爬取壁紙網(wǎng)站的示例

python 多線程爬取壁紙網(wǎng)站的示例

python 多線程爬取壁紙網(wǎng)站的示例

整體思路:

1、先在列表頁(yè)面獲取每張壁紙的詳情頁(yè)地址

2、在壁紙?jiān)斍轫?yè)面獲取壁紙真實(shí)高清url地址

3、保存地址

代碼實(shí)現(xiàn)

模擬瀏覽器請(qǐng)請(qǐng)求網(wǎng)頁(yè),獲取網(wǎng)頁(yè)數(shù)據(jù)

python 多線程爬取壁紙網(wǎng)站的示例

這里只選擇爬取前10頁(yè)的數(shù)據(jù)

代碼如下

import threadingimport parselimport requestsdef get_html(html_url): ’’’ 獲取網(wǎng)頁(yè)源代碼 :param html_url: 網(wǎng)頁(yè)url :return: ’’’ response = requests.get(url=html_url, headers=headers) return responsedef get_par(html_data): ’’’ 把 response.text 轉(zhuǎn)換成 selector 對(duì)象 解析提取數(shù)據(jù) :param html_data: response.text :return: selector 對(duì)象 ’’’ selector = parsel.Selector(html_data) return selectordef 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: 列表頁(yè)面 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)站的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 江口县| 山阳县| 衡山县| 达拉特旗| 固镇县| 泗阳县| 澄城县| 法库县| 崇州市| 桦川县| 平阳县| 阿拉善盟| 南涧| 达拉特旗| 岐山县| 沂水县| 张家川| 定远县| 建宁县| 惠水县| 泸定县| 邵东县| 焉耆| 应城市| 宁南县| 岗巴县| 武夷山市| 阿拉善右旗| 石阡县| 谷城县| 吴忠市| 峨边| 三明市| 赤峰市| 博客| 济宁市| 石门县| 韩城市| 泉州市| 抚州市| 太保市|