最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

爬取網(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)頁鏈接不完整,我們手動給它添加上去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í)候,就會返回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)文章

  • python將數(shù)據(jù)插入數(shù)據(jù)庫的代碼分享

    python將數(shù)據(jù)插入數(shù)據(jù)庫的代碼分享

    在本篇文章里小編給大家整理的是關(guān)于python將數(shù)據(jù)插入數(shù)據(jù)庫的代碼內(nèi)容,有興趣的朋友們可以參考下。
    2020-08-08
  • 詳解基于python的多張不同寬高圖片拼接成大圖

    詳解基于python的多張不同寬高圖片拼接成大圖

    這篇文章主要介紹了詳解基于python的多張不同寬高圖片拼接成大圖,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • python編程scrapy簡單代碼實(shí)現(xiàn)搜狗圖片下載器

    python編程scrapy簡單代碼實(shí)現(xiàn)搜狗圖片下載器

    這篇文章主要為大家介紹了使用python scrapy簡單代碼實(shí)現(xiàn)搜狗圖片下載器示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-11-11
  • python-sys.stdout作為默認(rèn)函數(shù)參數(shù)的實(shí)現(xiàn)

    python-sys.stdout作為默認(rèn)函數(shù)參數(shù)的實(shí)現(xiàn)

    今天小編就為大家分享一篇 python-sys.stdout作為默認(rèn)函數(shù)參數(shù)的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python如何進(jìn)行時(shí)間處理

    Python如何進(jìn)行時(shí)間處理

    這篇文章主要介紹了Python如何進(jìn)行時(shí)間處理,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-08-08
  • Python爬蟲之Selenium鼠標(biāo)事件的實(shí)現(xiàn)

    Python爬蟲之Selenium鼠標(biāo)事件的實(shí)現(xiàn)

    這篇文章主要介紹了Python爬蟲之Selenium鼠標(biāo)事件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 用python爬蟲批量下載pdf的實(shí)現(xiàn)

    用python爬蟲批量下載pdf的實(shí)現(xiàn)

    這篇文章主要介紹了用python爬蟲批量下載pdf的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • python使用7z解壓軟件備份文件腳本分享

    python使用7z解壓軟件備份文件腳本分享

    這篇文章主要介紹了python使用7z解壓軟件備份文件腳本,需要的朋友可以參考下
    2014-02-02
  • tensorflow pb to tflite 精度下降詳解

    tensorflow pb to tflite 精度下降詳解

    這篇文章主要介紹了tensorflow pb to tflite 精度下降詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • python exe文件解包方法總結(jié)

    python exe文件解包方法總結(jié)

    這篇文章總結(jié)了如何從Python EXE文件中提取和反編譯Python腳本(.pyc文件)的過程,包括使用pyinstxtractor.py或archive_viewer.py進(jìn)行解包,感興趣的小伙伴跟著小編一起來看看吧
    2025-03-03

最新評論

青冈县| 徐州市| 石家庄市| 长乐市| 柳州市| 乌兰浩特市| 拉孜县| 新巴尔虎左旗| 芦山县| 高尔夫| 漳州市| 芒康县| 本溪市| 广宁县| 屯留县| 抚州市| 沙洋县| 镶黄旗| 房产| 定襄县| 泸水县| 锦屏县| 南京市| 高安市| 成安县| 遵义县| 同仁县| 手游| 十堰市| 贵州省| 舞钢市| 弋阳县| 台南市| 遂平县| 黄陵县| 崇州市| 弋阳县| 新干县| 专栏| 嘉义县| 楚雄市|