今日、若者がチャットするとき、彼らは絵文字のない若者であると言うのが恥ずかしいです。絵文字は人と人とのチャットの不可欠な部分になっています。
会ったばかりの友達が数枚の絵文字を投げ出し、毎分関係を築きます。私のガールフレンドは2つの絵文字に不機嫌で満足しています。それはまた、困惑を解決することもできます。2つの絵文字全体を入力する時間がありません。
寿命が短すぎる、私はpythonを使用します
1つは、最初に宣伝したい
準備は非常に重要です。まず、何をするのか、何をするのか、どのように行うのかを知ってから、リアルタイムで段階的に進んで着実にプレイする必要があります。
開発環境の構成
Python 3.6
Pycharm
ブラウザを開き、インストールするソフトウェアの名前を検索します
Python
公式サイトは公式サイトです。名前の下に広告がある場合はクリックしないでください。自信を持って広告です。
以下のPython3.10.2をクリックするだけで、最新バージョンをダウンロードできます。[ダウンロード]をクリックする必要はありません。
pycharm
ダウンロード
ProfessionalEditionCommunityEditionをクリックするだけでOKです
インストール方法が長すぎて1つずつ説明できません。記事の下部にあるコードをスキャンして、ビデオを作成できます。
モジュールのインストール構成
requests
parsel
re
コンピューターの電源を入れ、win + rを押したまま、cmdと入力し、Enterキーを押し、pip install(およびインストールするモジュールの名前)を入力し、Enterキーを押してインストールします。
2.コード
目標:後ろのコードも含めて、全員がfabiaoqing
アドレスの表と裏を完成させてください。問題はないはずです。
インポートモジュール
import requests
import parsel
import re
import time
URLをリクエスト
url = f'fabiaoqing/biaoqing/lists/page/{page}.html'
リクエストヘッダー
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
}
Webページのソースコードに戻る
response = requests.get(url=url, headers=headers)
分析データ
selector = parsel.Selector(response.text) # 把respons.text 转换成 selector 对象
最初の抽出では、すべてのdivタグコンテンツが抽出されます
divs = selector.css('#container div.tagbqppdiv') # css 根据标签提取内容
タグコンテンツから彼の画像のURLアドレスを抽出します
img_url = div.css('img::attr(data-original)').get()
タイトルを抽出
title = div.css('img::attr(title)').get()
画像のサフィックス名を取得します
name = img_url.split('.')[-1]
データを保存する
new_title = change_title(title)
絵文字画像にリクエストを送信して、バイナリデータを取得します
img_content = requests.get(url=img_url, headers=headers).content
データを保存する
def save(title, img_url, name):
img_content = get_response(img_url).content
try:
with open('img\\' + title + '.' + name, mode='wb') as f:
# 写入图片二进制数据
f.write(img_content)
print('正在保存:', title)
except:
pass
タイトルの特殊文字を置き換える
ファイル名が不明で特殊文字が含まれているため、特殊文字を正規表現に置き換える必要があります。
def change_title(title):
mode = re.compile(r'[\\\/\:\*\?\"\<\>\|]')
new_title = re.sub(mode, "_", title)
return new_title
記録タイム
time_2 = time.time()
use_time = int(time_2) - int(time_1)
print(f'总共耗时:{use_time}秒')
ブラザーズ、これはシングルスレッドです。以下はマルチスレッドです。コードに直接移動します。
import requests
import parsel
import re
import time
import concurrent.futures
def change_title(title):
mode = re.compile(r'[\\\/\:\*\?\"\<\>\|]')
new_title = re.sub(mode, "_", title)
return new_title
def get_response(html_url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
}
repsonse = requests.get(url=html_url, headers=headers)
return repsonse
def save(title, img_url, name):
img_content = get_response(img_url).content
try:
with open('img\\' + title + '.' + name, mode='wb') as f:
f.write(img_content)
print('正在保存:', title)
except:
pass
def main(html_url):
html_data = get_response(html_url).text
selector = parsel.Selector(html_data)
divs = selector.css('#container div.tagbqppdiv')
for div in divs:
img_url = div.css('img::attr(data-original)').get()
title = div.css('img::attr(title)').get()
name = img_url.split('.')[-1]
new_title = change_title(title)
save(new_title, img_url, name)
if __name__ == '__main__':
time_1 = time.time()
exe = concurrent.futures.ThreadPoolExecutor(max_workers=10)
for page in range(1, 201):
url = f'fabiaoqing/biaoqing/lists/page/{page}.html'
exe.submit(main, url)
exe.shutdown()
time_2 = time.time()
use_time = int(time_2) - int(time_1)
print(f'总共耗时:{use_time}秒')
ブラザーズ、18秒で1,000枚以上の写真があります。これは少し速すぎて終了できません。
読んだ後で便利だと思ったら、気に入って保存してください。愛して触ってみてください。コードが実行されていることがわかります。とても速い。とても速い、良くない〜