python保存動態網頁,python將網頁保存為圖片

本文目錄一覽:

python 如何抓取動態頁面內容?

輸入url,得到html,我早就寫了函數了

自己搜:

getUrlRespHtml

就可以找到對應的python函數:

#——————————————————————————

def getUrlResponse(url, postDict={}, headerDict={}, timeout=0, useGzip=False, postDataDelimiter=””) :

    “””Get response from url, support optional postDict,headerDict,timeout,useGzip

    Note:

    1. if postDict not null, url request auto become to POST instead of default GET

    2  if you want to auto handle cookies, should call initAutoHandleCookies() before use this function.

       then following urllib2.Request will auto handle cookies

    “””

    # makesure url is string, not unicode, otherwise urllib2.urlopen will error

    url = str(url);

    if (postDict) :

        if(postDataDelimiter==””):

            postData = urllib.urlencode(postDict);

        else:

            postData = “”;

            for eachKey in postDict.keys() :

                postData += str(eachKey) + “=”  + str(postDict[eachKey]) + postDataDelimiter;

        postData = postData.strip();

        logging.info(“postData=%s”, postData);

        req = urllib2.Request(url, postData);

        logging.info(“req=%s”, req);

        req.add_header(‘Content-Type’, “application/x-www-form-urlencoded”);

    else :

        req = urllib2.Request(url);

    defHeaderDict = {

        ‘User-Agent’    : gConst[‘UserAgent’],

        ‘Cache-Control’ : ‘no-cache’,

        ‘Accept’        : ‘*/*’,

        ‘Connection’    : ‘Keep-Alive’,

    };

    # add default headers firstly

    for eachDefHd in defHeaderDict.keys() :

        #print “add default header: %s=%s”%(eachDefHd,defHeaderDict[eachDefHd]);

        req.add_header(eachDefHd, defHeaderDict[eachDefHd]);

    if(useGzip) :

        #print “use gzip for”,url;

        req.add_header(‘Accept-Encoding’, ‘gzip, deflate’);

    # add customized header later – allow overwrite default header 

    if(headerDict) :

        #print “added header:”,headerDict;

        for key in headerDict.keys() :

            req.add_header(key, headerDict[key]);

    if(timeout  0) :

        # set timeout value if necessary

        resp = urllib2.urlopen(req, timeout=timeout);

    else :

        resp = urllib2.urlopen(req);

        

    #update cookies into local file

    if(gVal[‘cookieUseFile’]):

        gVal[‘cj’].save();

        logging.info(“gVal[‘cj’]=%s”, gVal[‘cj’]);

    

    return resp;

#——————————————————————————

# get response html==body from url

#def getUrlRespHtml(url, postDict={}, headerDict={}, timeout=0, useGzip=False) :

def getUrlRespHtml(url, postDict={}, headerDict={}, timeout=0, useGzip=True, postDataDelimiter=””) :

    resp = getUrlResponse(url, postDict, headerDict, timeout, useGzip, postDataDelimiter);

    respHtml = resp.read();

    

    #here, maybe, even if not send Accept-Encoding: gzip, deflate

    #but still response gzip or deflate, so directly do undecompress

    #if(useGzip) :

    

    #print “—before unzip, len(respHtml)=”,len(respHtml);

    respInfo = resp.info();

    

    # Server: nginx/1.0.8

    # Date: Sun, 08 Apr 2012 12:30:35 GMT

    # Content-Type: text/html

    # Transfer-Encoding: chunked

    # Connection: close

    # Vary: Accept-Encoding

    # …

    # Content-Encoding: gzip

    

    # sometime, the request use gzip,deflate, but actually returned is un-gzip html

    # – response info not include above “Content-Encoding: gzip”

    # eg: 

    # – so here only decode when it is indeed is gziped data

    

    #Content-Encoding: deflate

    if(“Content-Encoding” in respInfo):

        if(“gzip” == respInfo[‘Content-Encoding’]):

            respHtml = zlib.decompress(respHtml, 16+zlib.MAX_WBITS);

        elif(“deflate” == respInfo[‘Content-Encoding’]):

            respHtml = zlib.decompress(respHtml, -zlib.MAX_WBITS);

    return respHtml;

及示例代碼:

url = “”;

respHtml = getUrlRespHtml(url);

完全庫函數,自己搜:

crifanLib.py

關於抓取動態頁面,詳見:

Python專題教程:抓取網站,模擬登陸,抓取動態網頁

(自己搜標題即可找到)

python怎麼獲取動態網頁鏈接?

四中方法:

”’

得到當前頁面所有連接

”’

import requests

import re

from bs4 import BeautifulSoup

from lxml import etree

from selenium import webdriver

url = ”

r = requests.get(url)

r.encoding = ‘gb2312’

# 利用 re

matchs = re.findall(r”(?=href=\”).+?(?=\”)|(?=href=\’).+?(?=\’)” , r.text)

for link in matchs:

print(link)

print()

# 利用 BeautifulSoup4 (DOM樹)

soup = BeautifulSoup(r.text,’lxml’)

for a in soup.find_all(‘a’):

link = a[‘href’]

print(link)

print()

# 利用 lxml.etree (XPath)

tree = etree.HTML(r.text)

for link in tree.xpath(“//@href”):

print(link)

print()

# 利用selenium(要開瀏覽器!)

driver = webdriver.Firefox()

driver.get(url)

for link in driver.find_elements_by_tag_name(“a”):

print(link.get_attribute(“href”))

driver.close()

如何用Python爬取動態加載的網頁數據

動態網頁抓取都是典型的辦法

直接查看動態網頁的加載規則。如果是ajax,則將ajax請求找出來給python。 如果是js去處後生成的URL。就要閱讀JS,搞清楚規則。再讓python生成URL。這就是常用辦法

辦法2,使用python調用webkit內核的,IE內核,或者是firefox內核的瀏覽器。然後將瀏覽結果保存下來。通常可以使用瀏覽器測試框架。它們內置了這些功能

辦法3,通過http proxy,抓取內容並進行組裝。甚至可以嵌入自己的js腳本進行hook. 這個方法通常用於系統的反向工程軟件

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/244467.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 13:02
下一篇 2024-12-12 13:02

相關推薦

  • 用Python繪製酷炫圖片

    在本篇文章中,我們將展示如何使用Python繪製酷炫的圖片。 一、安裝Python繪圖庫 在使用Python繪製圖片之前,我們需要先安裝Python繪圖庫。Python有很多繪圖庫…

    編程 2025-04-29
  • 使用axios獲取返回圖片

    使用axios獲取返回圖片是Web開發中很常見的需求。本文將介紹如何使用axios獲取返回圖片,並從多個方面進行詳細闡述。 一、安裝axios 使用axios獲取返回圖片前,首先需…

    編程 2025-04-29
  • Python 圖片轉表格

    本文將詳細介紹如何使用Python將圖片轉為表格。大家平時在處理一些資料的時候難免會遇到圖片轉表格的需求。比如從PDF文檔中提取表格等場景。當然,這個功能也可以通過手動複製、粘貼,…

    編程 2025-04-29
  • Python緩存圖片的處理方式

    本文將從多個方面詳細闡述Python緩存圖片的處理方式,包括緩存原理、緩存框架、緩存策略、緩存更新和緩存清除等方面。 一、緩存原理 緩存是一種提高應用程序性能的技術,在網絡應用中流…

    編程 2025-04-29
  • Python如何抓取圖片數據

    Python是一門強大的編程語言,能夠輕鬆地進行各種數據抓取與處理。抓取圖片數據是一個非常常見的需求。在這篇文章中,我們將從多個方面介紹Python如何抓取圖片數據。 一、使用ur…

    編程 2025-04-29
  • python爬取網頁並生成表格

    本文將從以下幾個方面詳細介紹如何使用Python爬取網頁數據並生成表格: 一、獲取網頁數據 獲取網頁數據的一般思路是通過HTTP請求獲取網頁內容,最常用的方式是使用Python庫r…

    編程 2025-04-28
  • Avue中如何按照後端返回的鏈接顯示圖片

    Avue是一款基於Vue.js、Element-ui等技術棧的可視化開發框架,能夠輕鬆搭建前端頁面。在開發中,我們使用到的圖片通常都是存儲在後端服務器上的,那麼如何使用Avue來展…

    編程 2025-04-28
  • Python利用Image加圖片的方法

    在Python中,利用Image庫可以快速處理圖片,並加入需要的圖片,本文將從多個方面詳細闡述這個操作。 一、Image庫的安裝和基礎操作 首先,我們需要在Python中安裝Ima…

    編程 2025-04-28
  • 網頁防篡改的重要性和市場佔有率

    網頁防篡改對於保護網站安全和用戶利益至關重要,而市場上針對網頁防篡改的產品和服務也呈現出不斷增長的趨勢。 一、市場佔有率 據不完全統計,目前全球各類網頁防篡改產品和服務的市場規模已…

    編程 2025-04-28
  • Python保存為二進制文件

    二進制文件是通過將整個文件內容轉化為二進制數據而生成的文件。Python具有將數據結構和對象存儲在二進制文件中的功能,提供了多種方法來實現這個目標,本文將從多個方面詳細闡述Pyth…

    編程 2025-04-28

發表回復

登錄後才能評論