《追火車的貓》表情包很火,python爬蟲爬一下當表情包

類似這種,正好看到有網頁有這種表情包的彙總:

https://mp.weixin.qq.com/s?__biz=MzA5MTY0NTYyOQ

爬它

# -*- coding:utf-8 -*-
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
import requests
import os

url  = input("請輸入百度圖片網址:")
FILENAME = input("請輸入要存放的文件夾名稱(切記要輸英文名!):")
browser = webdriver.Chrome()
wait = WebDriverWait(browser, 10)
browser.get(url)
time.sleep(5)
#這個階段自己拉網頁
# while cnt>0:
#     go_scroll(num,browser)
#     cnt = cnt-1
#     time.sleep(0.5)
html = browser.page_source
soup = BeautifulSoup(html, 'lxml')
images = soup.find_all('img', {'class': '__bg_gif'})

#figures = soup.find_all('imgitem')
#print(figures)
root = r'D:\crawl_download'
if os.path.exists(root) is not True:
    os.mkdir(root)
    print("root created!"+root)
src =""
path2 = os.path.join(root,FILENAME)
if os.path.exists(path2) is not True:
    os.mkdir(path2)
    print("百度圖片存放地址"+path2)
cnt = 1
for item in images:
    try:
        src= item['data-src']
        pic = requests.get(src).content
        print(src)
        cnt=cnt+1
        with open(os.path.join(path2,src[-50:-30])+'.gif','wb') as f:
            f.write(pic)
            f.close()
    except Exception:
        print(repr(Exception))
        continue
    print('download successful')
browser.close()

運行過程

結果:

over

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章