python re正则匹配网页中图片url地址的方法

 更新时间:2018年12月20日 09:28:26   作者:Arckal  
今天小编就为大家分享一篇python re正则匹配网页中图片url地址的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

最近写了个python抓取必应搜索首页http://cn.bing.com/的背景图片并将此图片更换为我的电脑桌面的程序,在正则匹配图片url时遇到了匹配失败问题。

要抓取的图片地址如图所示:

python re正则匹配网页中图片url地址

首先,使用这个pattern

reg = re.compile('.*g_img={url: "(http.*?jpg)"')

无论怎么匹配都匹配不到,后来把网页源码抓下来放在notepad++中查看,并用notepad++的正则匹配查找,很轻易就匹配到了,如图:

python re正则匹配网页中图片url地址

后来我写了个测试代码,把图片地址在的那一行保存在一个字符串中,很快就匹配到了,如下面代码所示,data是匹配不到的,然而line是可以匹配到的。

# -*-coding:utf-8-*-
import os
import re
 
f = open('bing.html','r')
 
line = r'''Bnp.Internal.Close(0,0,60056); } });;g_img={url: "https://az12410.vo.msecnd.net/homepage/app/2016hw/BingHalloween_BkgImg.jpg",id:'bgDiv',d:'200',cN'''
data = f.read().decode('utf-8','ignore').encode('gbk','ignore')
 
print " "
 
reg = re.compile('.*g_img={url: "(http.*?jpg)"')
 
if re.match(reg, data):
  m1 = reg.findall(data)
  print m1[0]
else:
  print("data Not match .")
  
print 20*'-'
#print line
if re.match(reg, line):
  m2 = reg.findall(line)
  print m2[0]
else:
  print("line Not match .")

由此可见line和data是有区别的,什么区别呢?那就是data是多行的,包含换行符,而line是单行的,没有换行符。我有在字符串line中加了换行符,结果line没有匹配到。

到这了原因就清楚了。原因就在这句话

re.compile('.*g_img={url: "(http.*?jpg)"')。

后来翻阅python文档,发现re.compile()这个函数的第二个可选参数flags。这个参数是re中定义的常量,有如下常量

re.DEBUG Display debug information about compiled expression.
re.I 
re.IGNORECASE Perform case-insensitive matching; expressions like [A-Z] will match lowercase letters, too. This is not affected by the current locale.
re.L 


re.LOCALE Make \w, \W, \b, \B, \s and \S dependent on the current locale.
re.M 


re.MULTILINE When specified, the pattern character '^' matches at the beginning of the string and at the beginning of each line (immediately following each newline); and the pattern character '$' matches at the end of the string and at the end of each line (immediately preceding each newline). By default, '^' matches only at the beginning of the string, and '$' only at the end of the string and immediately before the newline (if any) at the end of the string.

re.S 


re.DOTALL Make the '.' special character match any character at all, including a newline; without this flag, '.' will match anything except a newline.re.U re.UNICODE Make \w, \W, \b, \B, \d, \D, \s and \S dependent on the Unicode character properties database.New in version 2.0.
re.X 


re.VERBOSE This flag allows you to write regular expressions that look nicer and are more readable by allowing you to visually separate logical sections of the pattern and add comments. Whitespace within the pattern is ignored, except when in a character class or when preceded by an unescaped backslash. When a line contains a # that is not in a character class and is not preceded by an unescaped backslash, all characters from the leftmost such # through the end of the line are ignored.

这里我们需要的就是re.S 让'.'匹配所有字符,包括换行符。修改正则表达式为

reg = re.compile('.*g_img={url: "(http.*?jpg)"', re.S)

即可完美解决问题。

以上这篇python re正则匹配网页中图片url地址的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • python代数式括号有效性检验示例代码

    python代数式括号有效性检验示例代码

    这篇文章主要给大家介绍了关于python代数式括号有效性检验的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • Python如何在单元测试中给对象打补丁

    Python如何在单元测试中给对象打补丁

    这篇文章主要介绍了Python如何在单元测试中给对象打补丁,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-08-08
  • python中prettytable库的使用方法

    python中prettytable库的使用方法

    prettytable是Python的一个第三方工具库,用于创建漂亮的ASCII表格,本文主要介绍了python中prettytable库的使用方法,使用prettytable可以轻松地将数据可视化为表格,感兴趣的可以了解一下
    2023-08-08
  • 基于Python批量镶嵌拼接遥感影像/栅格数据(示例代码)

    基于Python批量镶嵌拼接遥感影像/栅格数据(示例代码)

    这篇文章主要介绍了基于Python批量镶嵌拼接遥感影像/栅格数据,使用时直接修改Mosaic_GDAL函数的入参就行了,选择数据存放的路径会自动拼接,命名也会自己设置无需额外修改,需要的朋友可以参考下
    2023-10-10
  • Django实现学员管理系统

    Django实现学员管理系统

    这篇文章主要为大家详细介绍了Django实现学员管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • python读取pdf格式文档的实现代码

    python读取pdf格式文档的实现代码

    这篇文章主要给大家介绍了关于python读取pdf格式文档的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • 使用NumPy进行数组数据处理的示例详解

    使用NumPy进行数组数据处理的示例详解

    NumPy是Python中用于数值计算的核心包之一,它提供了大量的高效数组操作函数和数学函数,可以支持多维数组和矩阵运算。本文主要为大家介绍了NumPy进行数组数据处理的具体方法,需要的可以参考一下
    2023-03-03
  • Python中使用 Selenium 实现网页截图实例

    Python中使用 Selenium 实现网页截图实例

    这篇文章主要介绍了Python中使用 Selenium 实现网页截图实例,Selenium支持Java、C#、Ruby 以及 Python等语言,本文以Python语言为例,需要的朋友可以参考下
    2014-07-07
  • Python3.9最新版下载与安装图文教程详解(Windows系统为例)

    Python3.9最新版下载与安装图文教程详解(Windows系统为例)

    这篇文章主要介绍了Python3.9最新版下载与安装图文教程详解,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • Pytorch基本变量类型FloatTensor与Variable用法

    Pytorch基本变量类型FloatTensor与Variable用法

    今天小编就为大家分享一篇Pytorch基本变量类型FloatTensor与Variable用法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-01-01

最新评论