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

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

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

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

使用工具:pycharm,requests

進入網(wǎng)頁

打開開發(fā)者工具

點擊 Network

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

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

滑到最下有一個User-Agent,復制

 向服務器發(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必須先導入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()

 進入其中一個網(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())

運行后同樣發(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
 

當爬到 奈克斯特奧特曼的時候,就會返回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)文章

  • 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的多張不同寬高圖片拼接成大圖,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-09-09
  • python編程scrapy簡單代碼實現(xiàn)搜狗圖片下載器

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

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

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

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

    Python如何進行時間處理

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

    Python爬蟲之Selenium鼠標事件的實現(xiàn)

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

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

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

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

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

    tensorflow pb to tflite 精度下降詳解

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

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

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

最新評論

津市市| 宣化县| 临武县| 南昌市| 陇南市| 肇源县| 达拉特旗| 黎川县| 略阳县| 多伦县| 敦化市| 宝清县| 临江市| 玉门市| 平顶山市| 霍山县| 荃湾区| 潼关县| 汉川市| 容城县| 安化县| 家居| 沐川县| 密山市| 江永县| 杭锦后旗| 临潭县| 松江区| 商南县| 开封市| 大厂| 十堰市| 金川县| 饶阳县| 商水县| 淳化县| 赤峰市| 浦城县| 自贡市| 昌宁县| 伊宁市|