您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“如何使用selenium爬取表情包”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“如何使用selenium爬取表情包”吧!
https://www.doutula.com/photo/list/?page=1
不過(guò)這個(gè)網(wǎng)站直接requests請(qǐng)求不到內(nèi)容,應(yīng)該也是js加載的。所以最簡(jiǎn)單的辦法就是使用selenium,萬(wàn)能!
該網(wǎng)站的結(jié)構(gòu)
如果是gif,會(huì)多出一個(gè)img標(biāo)簽
根據(jù)此來(lái)寫(xiě)出xpath,拿出圖片的地址,使用urllib進(jìn)行下載
下載完成后保存。然后在通過(guò)wxpy庫(kù),發(fā)送給好友或者群。
具體代碼
使用selenium獲取表情包,并保存到文件中。注意保存的時(shí)候文件名最好是英文或者數(shù)字,否則當(dāng)你使用wxpy庫(kù)模擬發(fā)送的時(shí)候,會(huì)直接報(bào)錯(cuò)。
def get_doutu():
browser = webdriver.Chrome()
# img_url_dic = {}
for i in range(1, 2):
browser.get('https://www.doutula.com/photo/list/?page=%s' % str(i))
time.sleep(4)
eles = browser.find_elements_by_xpath('//div[@class="container_"]/div[@id="pic-detail"]/div[@class="row"]/div[@class="col-sm-9"]/div[@class="random_picture"]/ul[@class="list-group"]/li[@class="list-group-item"]/div[@class="page-content text-center"]/div/a[@class="col-xs-6 col-sm-3"]/img[@data-original]')
# names = browser.find_elements_by_xpath('//div[@class="container_"]/div[@id="pic-detail"]/div[@class="row"]/div[@class="col-sm-9"]/div[@class="random_picture"]/ul[@class="list-group"]/li[@class="list-group-item"]/div[@class="page-content text-center"]/div/a[@class="col-xs-6 col-sm-3"]/img[@data-original]')
for j in range(len(eles)):
url = eles[j].get_attribute('data-original')
url = url.split('!')[0]
ext = url.split('.')[-1]
name = random.randrange(1, 10000, 2)
filename = str(name) + '.' + ext
# 保存圖片數(shù)據(jù)
data = urllib.request.urlopen(url).read()
f = open('image/' + filename, 'wb')
f.write(data)
f.close()
time.sleep(5)
browser.close()
browser.quit()
獲取文件列表,打印是為了充數(shù),將文件的地址拼接好,發(fā)送給send_news(),由它進(jìn)行發(fā)送。
def listdir(file_dir):
for root, dirs, files in os.walk(file_dir):
print(root) # 當(dāng)前目錄路徑
print(dirs) # 當(dāng)前路徑下所有子目錄
print(files) # 當(dāng)前路徑下所有非目錄子文件
print(type(files))
for i in files:
send_news(file_dir + i)
time.sleep(10)
到入wxpy包,登錄微信并發(fā)送
def login_wechat():
global bot
bot = Bot()
# bot = Bot(console_qr=2,cache_path="botoo.pkl")#linux環(huán)境上使用
def send_news(data):
if bot == None:
login_wechat()
try:
my_group = bot.groups().search(u'逗比四人行')[0]
time.sleep(20)
print(data)
my_group.send_image(data)
except Exception as e:
print(u"失?。?!", e)
最好設(shè)置間隔時(shí)間。小心被清理。
到此,相信大家對(duì)“如何使用selenium爬取表情包”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。