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线程的两种编程方式,Python中如果要使用线程的话,一种是函数式,一种是用类来包装的线程对象,需要的朋友可以参考下
    2015-04-04
  • Pytorch中关于inplace的操作

    Pytorch中关于inplace的操作

    这篇文章主要介绍了Pytorch中关于inplace的操作方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • 使用Pandas对列名和索引进行重命名的几种常见方法

    使用Pandas对列名和索引进行重命名的几种常见方法

    在数据分析和处理中,Pandas是一个非常强大的工具,它提供了灵活的数据结构和丰富的操作方法,使得数据处理变得更加简单高效,其中,对数据的列名和索引进行重命名是常见的需求之一,本文将从基础概念出发,逐步深入探讨如何使用Pandas对列名和索引进行重命名
    2024-12-12
  • pymssql ntext字段调用问题解决方法

    pymssql ntext字段调用问题解决方法

    pymssql是python用来连接mssql数据库的一个类库。该库遵守Python DB API 2.0 标准,并且还附带了一个原生的低阶数据访问模块。
    2008-12-12
  • Windows下Eclipse+PyDev配置Python+PyQt4开发环境

    Windows下Eclipse+PyDev配置Python+PyQt4开发环境

    这篇文章主要介绍了Windows下Eclipse+PyDev配置Python+PyQt4开发环境的相关资料,需要的朋友可以参考下
    2016-05-05
  • Python时间戳与日期格式之间相互转化的详细教程

    Python时间戳与日期格式之间相互转化的详细教程

    java默认精度是毫秒级别的,生成的时间戳是13位,而python默认是10位的,精度是秒,下面这篇文章主要给大家介绍了关于Python时间戳与日期格式之间相互转化的相关资料,需要的朋友可以参考下
    2022-08-08
  • Python入门(六)Python数据类型

    Python入门(六)Python数据类型

    这篇文章主要介绍了Python入门(六)Python数据类型,Python是一门非常强大好用的语言,也有着易上手的特性,本文为入门教程,需要的朋友可以参考下
    2023-04-04
  • Python的组合模式与责任链模式编程示例

    Python的组合模式与责任链模式编程示例

    这篇文章主要介绍了Python的组合模式与责任链模式编程示例,组合模式与责任链模式都属于Python的设计模式,需要的朋友可以参考下
    2016-02-02
  • PyTorch如何创建自己的数据集

    PyTorch如何创建自己的数据集

    这篇文章主要介绍了PyTorch如何创建自己的数据集,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • python编码格式导致csv读取错误问题(csv.reader, pandas.csv_read)

    python编码格式导致csv读取错误问题(csv.reader, pandas.csv_read)

    python编码格式导致csv读取错误问题(csv.reader, pandas.csv_read),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05

最新评论