Python 網路爬蟲 爬取表情包

2021-08-21 04:05:52 字數 1814 閱讀 6062

import requests

import os

import urllib.parse

from bs4 import beautifulsoup

# 函式:判斷表情包是不是0個

def is_zero(url):

test = requests.get(url)

msg = test.content

s = beautifulsoup(msg, 'html.parser')

menu = s.find_all(attrs=)

tip = menu[0].text.strip()

return tip

def make_dir(key):

if not os.path.exists(key):

os.mkdir(key)

def down_img(url, key):

# 拿到所有頁碼的表情

page = 1

# 定義變數,作為名稱

count = 1

# 定義列表存放位址

img_list =

# 定義標誌表示程式是否執行

flag = true

while true:

url += '/type/bq/page/%d.html' % page

page += 1

# 爬蟲網頁,獲取網頁html標籤

response = requests.get(url)

content = response.content

# 通過網頁內容找到位址列表

soup = beautifulsoup(content, 'html.parser')

result = soup.findall(attrs=)

# 迴圈遍歷列表

for i in result:

# 拿到位址與盛放path的列表進行比對,如果存在就終止程式

path = i['data-original']

if path not in img_list:

# 判斷檔名是不是http://開頭,沒有就加上

# 寫入檔案

with open(key + '/' + str(count) + '.gif', 'wb') as fw:

fw.write(img_msg.content)

count += 1

else:

flag = false

if not flag:

break

# 主函式

def main():

key = input('----->請輸入:')

keyword = urllib.parse.quote(key)

url = "/search/search/keyword/" + keyword

if is_zero(url) == '表情(0)':

# 判斷如果表情是0,就返回一句話

print('哦 no 我們沒有%s的表情包' % key)

else:

# 呼叫函式建立資料夾

make_dir(key)

down_img(url, key)

if __name__ == '__main__':

main()

input('按回車鍵退出----')

python爬蟲 表情包爬取

問題 1.路徑問題以及獲取路徑問題 注 argv 0 只是得到的是當前指令碼的絕對位置 而os模組中的幾種獲得路徑的方法,得到的是當前的工作目錄,如 open 1.txt r 則會在當前工作目錄查詢該檔案。即大部分的檔案操作都是相對於當前工作路徑。若要改變當前工作路徑,可以用 os.chdir pa...

爬蟲 學習爬取表情包

表情包位址 表情位址 1.請求目標 2.匹配不同位址 url def get urls url 1.請求目標 response requests.get url 2.通過正則來匹配不同位址 url 每張共有內容保留,不同的內容用.來匹配 表示匹配任意數量不換行的字元 表示盡可能匹配最短的字元 r u...

爬取表情包

這是我第一次在這寫部落格,還是有點興奮。我也是剛接觸python不久,發現python 真的很強大,簡單就可以處理複雜的事。最近很想寫個爬蟲,但水平沒達到,正好csdn學院有個公開課,黃勇老師講的 90分鐘掌握python多執行緒爬蟲 全程實戰 3月6日晚20 00我聽了直播,當時沒跟上,等看了回播...