一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術(shù)及教程分享平臺(tái)!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - Python實(shí)現(xiàn)多線程爬表情包詳解

Python實(shí)現(xiàn)多線程爬表情包詳解

2022-03-08 00:14魔王不會(huì)哭 Python

這篇文章主要介紹了Python多線程爬表情包,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

課程亮點(diǎn)

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

html標(biāo)簽數(shù)據(jù)解析方法

海量圖片數(shù)據(jù)一鍵保存

環(huán)境介紹

python 3.8

pycharm

模塊使用

requests >>> pip install requests

parsel >>> pip install parsel

time 時(shí)間模塊 記錄運(yùn)行時(shí)間

流程

一. 分析我們想要的數(shù)據(jù)內(nèi)容 是可以從哪里獲取

表情包 >>> 圖片url地址 以及 圖片名字

對(duì)于開發(fā)者工具的使用 >>>

二. 代碼實(shí)現(xiàn)步驟

1.發(fā)送請(qǐng)求

確定一下發(fā)送請(qǐng)求 url地址

請(qǐng)求方式是什么 get請(qǐng)求方式 post請(qǐng)求方式

請(qǐng)求頭參數(shù) : 防盜鏈 cookie …

2.獲取數(shù)據(jù)

獲取服務(wù)器返回的數(shù)據(jù)內(nèi)容

response.text 獲取文本數(shù)據(jù)

response.json() 獲取json字典數(shù)據(jù)

response.content 獲取二進(jìn)制數(shù)據(jù) 保存圖片/音頻/視頻/特定格式文件內(nèi)容 都是獲取二進(jìn)制數(shù)據(jù)內(nèi)容

3.解析數(shù)據(jù)

提取我們想要的數(shù)據(jù)內(nèi)容

I. 可以直接解析處理

II. json字典數(shù)據(jù) 鍵值對(duì)取值

III. re正則表達(dá)式

IV. css選擇器

V. xpath

4.保存數(shù)據(jù)

文本

csv

數(shù)據(jù)庫(kù)

本地文件夾

導(dǎo)入模塊

?
1
2
3
4
5
import requests  # 數(shù)據(jù)請(qǐng)求模塊 第三方模塊 pip install requests
import parsel  # 數(shù)據(jù)解析模塊 第三方模塊 pip install parsel
import re  # 正則表達(dá)式模塊
import time  # 時(shí)間模塊
import concurrent.futures

單線程爬取10頁(yè)數(shù)據(jù)

1. 發(fā)送請(qǐng)求

?
1
2
3
4
5
6
7
8
9
start_time = time.time()
 
for page in range(1, 11):
    url = f'https://fabiaoqing.com/biaoqing/lists/page/{page}html'
     headers = {
         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36'
     }
     response = requests.get(url=url, headers=headers)
     # <Response [200]> response 對(duì)象 200狀態(tài)碼 表示請(qǐng)求成功

2. 獲取數(shù)據(jù), 獲取文本數(shù)據(jù) / 網(wǎng)頁(yè)源代碼

?
1
2
3
4
# 在開發(fā)者工具上面 元素面板 看到有相應(yīng)標(biāo)簽數(shù)據(jù), 但是我發(fā)送請(qǐng)求之后 沒有這樣的數(shù)據(jù)返回
# 我們要提取數(shù)據(jù), 要根據(jù)服務(wù)器返回?cái)?shù)據(jù)內(nèi)容
# xpath 解析方法 parsel 解析模塊  parsel這個(gè)模塊里面就可以調(diào)用xpath解析方法
# print(response.text)

3. 解析數(shù)據(jù)

?
1
2
3
4
5
6
7
# 解析速度 bs4 解析速度會(huì)慢一些 如果你想要對(duì)于字符串?dāng)?shù)據(jù)內(nèi)容 直接取值 只能正則表達(dá)式
     selector = parsel.Selector(response.text) # 把獲取下來html字符串?dāng)?shù)據(jù)內(nèi)容 轉(zhuǎn)成 selector 對(duì)象
     title_list = selector.css('.ui.image.lazy::attr(title)').getall()
     img_list = selector.css('.ui.image.lazy::attr(data-original)').getall()
# 把獲取下來的這兩個(gè)列表 提取里面元素 一一提取出來
# 提取列表元素 for循環(huán) 遍歷
     for title, img_url in zip(title_list, img_list):

4. 保存數(shù)據(jù)

?
1
2
3
4
5
6
7
8
9
10
11
# split() 字符串分割的方法 根據(jù)列表索引位置取值
# img_name_1 = img_url[-3:] # 通過字符串?dāng)?shù)據(jù) 進(jìn)行切片
# 從左往右 索引位置 是從 0 開始 從右往左 是 -1開始
         # print(title, img_url)
         title = re.sub(r'[\/:*?"<>|\n]', '_', title)
         # 名字太長(zhǎng) 報(bào)錯(cuò)
         img_name = img_url.split('.')[-1]   # 通過split() 字符串分割的方法 根據(jù)列表索引位置取值
         img_content = requests.get(url=img_url).content # 獲取圖片的二進(jìn)制數(shù)據(jù)內(nèi)容
         with open('img\\' + title + '.' + img_name, mode='wb') as f:
             f.write(img_content)
         print(title)

多線程爬取10頁(yè)數(shù)據(jù)

?
1
2
3
4
5
6
7
def get_response(html_url):
    """發(fā)送請(qǐng)求"""
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36'
    }
    response = requests.get(url=html_url, headers=headers)
    return response
?
1
2
3
4
5
6
7
8
def get_img_info(html_url):
    """獲取圖片url地址 以及 圖片名字"""
    response = get_response(html_url)
    selector = parsel.Selector(response.text)  # 把獲取下來html字符串?dāng)?shù)據(jù)內(nèi)容 轉(zhuǎn)成 selector 對(duì)象
    title_list = selector.css('.ui.image.lazy::attr(title)').getall()
    img_list = selector.css('.ui.image.lazy::attr(data-original)').getall()
    zip_data = zip(title_list, img_list)
    return zip_data
?
1
2
3
4
5
6
7
8
9
def save(title, img_url):
    """保存數(shù)據(jù)"""
    title = re.sub(r'[\/:*?"<>|\n]', '_', title)
    # 名字太長(zhǎng) 報(bào)錯(cuò)
    img_name = img_url.split('.')[-1# 通過split() 字符串分割的方法 根據(jù)列表索引位置取值
    img_content = requests.get(url=img_url).content  # 獲取圖片的二進(jìn)制數(shù)據(jù)內(nèi)容
    with open('img\\' + title + '.' + img_name, mode='wb') as f:
        f.write(img_content)
    print(title)

多進(jìn)程爬取10頁(yè)數(shù)據(jù)

?
1
2
3
4
def main(html_url):
    zip_data = get_img_info(html_url)
    for title, img_url in zip_data:
        save(title, img_url)
?
1
2
3
4
5
6
7
8
9
10
11
if __name__ == '__main__':
    start_time = time.time()
    exe = concurrent.futures.ThreadPoolExecutor(max_workers=10)
    for page in range(1, 11):
        # 1. 發(fā)送請(qǐng)求
        url = f'https://fabiaoqing.com/biaoqing/lists/page/{page}html'
        exe.submit(main, url)
    exe.shutdown()
    end_time = time.time()
    use_time = int(end_time - start_time)
    print('程序耗時(shí): ', use_time)

單線程爬取10頁(yè)數(shù)據(jù) 61秒時(shí)間

多線程爬取10頁(yè)數(shù)據(jù) 19秒時(shí)間 >>> 13

多進(jìn)程爬取10頁(yè)數(shù)據(jù) 21秒時(shí)間 >>> 18

到此這篇關(guān)于Python實(shí)現(xiàn)多線程爬表情包詳解的文章就介紹到這了,更多相關(guān)Python 多線程爬表情包內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://blog.csdn.net/python56123/article/details/121471909?spm=1001.2014.3001.5501

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 深夜在线网站 | 色老妈| 为什么丈夫插我我却喜欢被打着插 | 国产探花视频在线观看 | 国产欧美日韩在线观看精品 | 精品国产香蕉 | 国产亚洲精品线观看77 | 免费一级国产大片 | 国产欧美精品专区一区二区 | 亚洲日本免费 | 免费观看视频网站 | 羞羞一区二区三区四区片 | 91久久精品视频 | 小柔的性放荡羞辱日记动漫 | 亚洲美女aⅴ久久久91 | 草逼的视频 | 国产成人免费在线观看 | 强制高h| 婷婷婷色 | 国产亚洲女在线精品 | gay中国| 亚洲美色综合天天久久综合精品 | 男人叼女人的痛爽视频免费 | 全程粗语对白视频videos | 校服下的白嫩小乳尖h1v1 | 成人特级毛片69免费观看 | 日韩性公交车上xxhd免费 | 男女发生性关系视频 | 国产精品第四页 | 色噜噜视频影院 | 国产视频91在线 | 69萝莉| 亚洲免费色图 | 日本漫画无翼乌 | 狠狠色婷婷日日综合五月 | 国产欧美一区二区三区免费看 | 亚洲欧洲日产国码无码av | 欧美成狂野欧美在线观看 | 久久人妻少妇嫩草AV無碼 | 国产欧美一区二区精品性色99 | 九九在线精品亚洲国产 |