亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

用python實現(xiàn)爬取奧特曼圖片實例

 更新時間:2022年02月11日 14:48:24   作者:K5679527  
大家好,本篇文章主要講的是用python實現(xiàn)爬取奧特曼圖片實例,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下

爬取網(wǎng)址:http://www.ultramanclub.com/allultraman/

使用工具:pycharm,requests

進(jìn)入網(wǎng)頁

打開開發(fā)者工具

點(diǎn)擊 Network

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

其中的Request URL就是我們所爬取的網(wǎng)址

滑到最下有一個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')  #獲取三個時代的網(wǎng)頁鏈接
 
for period_href in period_hrefs:
    print(period_href.get())
 

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

 進(jìn)入其中一個網(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')  #獲取三個時代的網(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('./','')     #獲取每個奧特曼的網(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)爬到 奈克斯特奧特曼的時候,就會返回None,調(diào)了半天,也沒搞懂,所以用if url:語句跳過了奈克斯特奧特曼,有沒有大佬知道原因

url = li_selector.xpath('//div[@class="left"]/figure/img/@src').get()

到此這篇關(guān)于用python實現(xiàn)爬取奧特曼圖片實例的文章就介紹到這了,更多相關(guān)python爬取奧特曼圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • tensorflow自定義激活函數(shù)實例

    tensorflow自定義激活函數(shù)實例

    今天小編就為大家分享一篇tensorflow自定義激活函數(shù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python中使用遍歷在列表中添加字典遇到的坑

    Python中使用遍歷在列表中添加字典遇到的坑

    今天小編就為大家分享一篇關(guān)于Python中使用遍歷在列表中添加字典遇到的坑,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • 關(guān)于Python中compile() 函數(shù)簡單實用示例詳解

    關(guān)于Python中compile() 函數(shù)簡單實用示例詳解

    這篇文章主要介紹了關(guān)于compile() 函數(shù)簡單實用示例,compile() 函數(shù)將一個字符串編譯為字節(jié)代碼,compile將代碼編譯為代碼對象,應(yīng)用在代碼中可以提高效率,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • 使用Python實現(xiàn)計算DICOM圖像兩點(diǎn)真實距離

    使用Python實現(xiàn)計算DICOM圖像兩點(diǎn)真實距離

    這篇文章主要為大家詳細(xì)介紹了如何使用Python實現(xiàn)計算DICOM圖像兩點(diǎn)真實距離,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-11-11
  • 使用python實現(xiàn)UDP通信方式

    使用python實現(xiàn)UDP通信方式

    這篇文章主要介紹了使用python實現(xiàn)UDP通信方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Python自動化構(gòu)建工具scons使用入門筆記

    Python自動化構(gòu)建工具scons使用入門筆記

    這篇文章主要介紹了Python自動化構(gòu)建工具scons使用入門筆記,本文講解了安裝scons、scons常用命令、scons使用示例等內(nèi)容,需要的朋友可以參考下
    2015-03-03
  • 基于Python的XSS測試工具XSStrike使用方法

    基于Python的XSS測試工具XSStrike使用方法

    XSS(Cross Site Scripting,跨站腳本攻擊)是一類特殊的Web客戶端腳本注入攻擊手段,通常指攻擊者通過“HTML注入”篡改了網(wǎng)頁,插入惡意的腳本,從而在用戶瀏覽網(wǎng)頁時控制瀏覽器的一種攻擊。
    2017-07-07
  • Python數(shù)據(jù)分析numpy的Nan和Inf使用注意點(diǎn)詳解

    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)建方式

    本文主要介紹了詳解解Django 多對多表關(guān)系的三種創(chuàng)建方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • Django media static外部訪問Django中的圖片設(shè)置教程

    Django media static外部訪問Django中的圖片設(shè)置教程

    這篇文章主要介紹了Django media static外部訪問Django中的圖片設(shè)置教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04

最新評論