用python實(shí)現(xiàn)爬取奧特曼圖片實(shí)例
爬取網(wǎng)址:http://www.ultramanclub.com/allultraman/
使用工具:pycharm,requests
進(jìn)入網(wǎng)頁

打開開發(fā)者工具

點(diǎn)擊 Network

刷新網(wǎng)頁,獲取信息


其中的Request URL就是我們所爬取的網(wǎng)址
滑到最下有一個(gè)User-Agent,復(fù)制

向服務(wù)器發(fā)送請求

200意味著請求成功
使用 response.text 獲取文本數(shù)據(jù)

可以看到有些亂碼
使用encode轉(zhuǎn)換
import requests
url = 'http://www.ultramanclub.com/allultraman/'
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36'
}
response = requests.get(url = url,headers=headers)
html = response.text
Html=html.encode('iso-8859-1').decode('gbk')
print(Html)
接下來開始爬取需要的數(shù)據(jù)
使用Xpath獲得網(wǎng)頁鏈接
要使用Xpath必須先導(dǎo)入parsel包
import requests
import parsel
def get_response(html_url):
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36'
}
response = requests.get(url = html_url,headers=headers)
return response
url = 'http://www.ultramanclub.com/allultraman/'
response = get_response(url)
html=response.text.encode('iso-8859-1').decode('gbk')
selector = parsel.Selector(html)
period_hrefs = selector.xpath('//div[@class="btn"]/a/@href') #獲取三個(gè)時(shí)代的網(wǎng)頁鏈接
for period_href in period_hrefs:
print(period_href.get())

可以看到網(wǎng)頁鏈接不完整,我們手動(dòng)給它添加上去period_href = 'http://www.ultramanclub.com/allultraman/' + period_href.get()

進(jìn)入其中一個(gè)網(wǎng)頁

跟之前的操作一樣,用Xpath獲取奧特曼的網(wǎng)頁信息
for period_href in period_hrefs:
period_ + period_href.get()
# print(period_href)
period_response = get_response(period_href).text
period_html = parsel.Selector(period_response)
lis = period_html.xpath('//div[@class="ultraheros-Contents_Generations"]/div/ul/li/a/@href')
for li in lis:
print(li.get())運(yùn)行后同樣發(fā)現(xiàn)鏈接不完整

li = 'http://www.ultramanclub.com/allultraman/' + li.get().replace('./','')

拿到網(wǎng)址后繼續(xù)套娃操作,就可以拿到圖片數(shù)據(jù)

png_url = 'http://www.ultramanclub.com/allultraman/' + li_selector.xpath('//div[@class="left"]/figure/img/@src').get().replace('../','')

完整代碼
import requests
import parsel
import os
dirname = "奧特曼"
if not os.path.exists(dirname): #判斷是否存在名稱為奧特曼的文件夾,沒有就創(chuàng)建
os.mkdir(dirname)
def get_response(html_url):
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36'
}
response = requests.get(url = html_url,headers=headers)
return response
url = 'http://www.ultramanclub.com/allultraman/'
response = get_response(url)
html=response.text.encode('iso-8859-1').decode('gbk')
selector = parsel.Selector(html)
period_hrefs = selector.xpath('//div[@class="btn"]/a/@href') #獲取三個(gè)時(shí)代的網(wǎng)頁鏈接
for period_href in period_hrefs:
period_ + period_href.get()
period_html = get_response(period_href).text
period_selector = parsel.Selector(period_html)
lis = period_selector.xpath('//div[@class="ultraheros-Contents_Generations"]/div/ul/li/a/@href')
for li in lis:
li = 'http://www.ultramanclub.com/allultraman/' + li.get().replace('./','') #獲取每個(gè)奧特曼的網(wǎng)址
# print(li)
li_html = get_response(li).text
li_selector = parsel.Selector(li_html)
url = li_selector.xpath('//div[@class="left"]/figure/img/@src').get()
# print(url)
if url:
png_url = 'http://www.ultramanclub.com/allultraman/' + url.replace('.', '')
png_title =li_selector.xpath('//ul[@class="lists"]/li[3]/text()').get()
png_title = png_title.encode('iso-8859-1').decode('gbk')
# print(li,png_title)
png_content = get_response(png_url).content
with open(f'{dirname}\\{png_title}.png','wb') as f:
f.write(png_content)
print(png_title,'圖片下載完成')
else:
continue


當(dāng)爬到 奈克斯特奧特曼的時(shí)候,就會(huì)返回None,調(diào)了半天,也沒搞懂,所以用if url:語句跳過了奈克斯特奧特曼,有沒有大佬知道原因
url = li_selector.xpath('//div[@class="left"]/figure/img/@src').get()到此這篇關(guān)于用python實(shí)現(xiàn)爬取奧特曼圖片實(shí)例的文章就介紹到這了,更多相關(guān)python爬取奧特曼圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Python中compile() 函數(shù)簡單實(shí)用示例詳解
這篇文章主要介紹了關(guān)于compile() 函數(shù)簡單實(shí)用示例,compile() 函數(shù)將一個(gè)字符串編譯為字節(jié)代碼,compile將代碼編譯為代碼對象,應(yīng)用在代碼中可以提高效率,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
使用Python實(shí)現(xiàn)計(jì)算DICOM圖像兩點(diǎn)真實(shí)距離
這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)計(jì)算DICOM圖像兩點(diǎn)真實(shí)距離,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11
Python自動(dòng)化構(gòu)建工具scons使用入門筆記
這篇文章主要介紹了Python自動(dòng)化構(gòu)建工具scons使用入門筆記,本文講解了安裝scons、scons常用命令、scons使用示例等內(nèi)容,需要的朋友可以參考下2015-03-03
Python數(shù)據(jù)分析numpy的Nan和Inf使用注意點(diǎn)詳解
這篇文章主要為大家介紹了Python數(shù)據(jù)分析numpy的Nan和Inf使用注意點(diǎn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
詳解解Django 多對多表關(guān)系的三種創(chuàng)建方式
本文主要介紹了詳解解Django 多對多表關(guān)系的三種創(chuàng)建方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08
Django media static外部訪問Django中的圖片設(shè)置教程
這篇文章主要介紹了Django media static外部訪問Django中的圖片設(shè)置教程,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04

