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/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

发表回复

登录后才能评论