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


Posted in Python onDecember 20, 2018

最近写了个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常规方法实现数组的全排列
Mar 17 Python
python读取txt文件,去掉空格计算每行长度的方法
Dec 20 Python
如何在django里上传csv文件并进行入库处理的方法
Jan 02 Python
python 堆和优先队列的使用详解
Mar 05 Python
python使用thrift教程的方法示例
Mar 21 Python
Python操作redis实例小结【String、Hash、List、Set等】
May 16 Python
selenium获取当前页面的url、源码、title的方法
Jun 12 Python
解决Python import docx出错DLL load failed的问题
Feb 13 Python
详解PyQt5信号与槽的几种高级玩法
Mar 24 Python
pycharm下配置pyqt5的教程(anaconda虚拟环境下+tensorflow)
Mar 25 Python
python导入库的具体方法
Jun 18 Python
Python语法学习之进程的创建与常用方法详解
Apr 08 Python
python使用pdfminer解析pdf文件的方法示例
Dec 20 #Python
python爬取指定微信公众号文章
Dec 20 #Python
在Django中URL正则表达式匹配的方法
Dec 20 #Python
python采集微信公众号文章
Dec 20 #Python
Linux下Pycharm、Anaconda环境配置及使用踩坑
Dec 19 #Python
python爬虫之urllib,伪装,超时设置,异常处理的方法
Dec 19 #Python
python3实现网络爬虫之BeautifulSoup使用详解
Dec 19 #Python
You might like
php比较多维数组中值的大小排序实现代码
2012/09/08 PHP
PHP以指定字段为索引返回数据库所取的数据数组
2013/06/30 PHP
PHP获取一年中每个星期的开始和结束日期的方法
2015/02/12 PHP
PHP使用Pthread实现的多线程操作实例
2015/11/14 PHP
Javascript 面向对象 继承
2010/05/13 Javascript
javascript与CSS复习(二)
2010/06/29 Javascript
jQuery 表单验证扩展代码(一)
2010/10/11 Javascript
jquery中dom操作和事件的实例学习 仿yahoo邮箱登录框的提示效果
2011/11/30 Javascript
5个可以帮你理解JavaScript核心闭包和作用域的小例子
2014/10/08 Javascript
AngularJS国际化详解及示例代码
2016/08/18 Javascript
JQuery实现DIV其他动画效果的简单实例
2016/09/18 Javascript
ionic 3.0+ 项目搭建运行环境的教程
2017/08/09 Javascript
微信小程序日期时间选择器使用方法
2018/02/01 Javascript
Angular4.x通过路由守卫进行路由重定向实现根据条件跳转到相应的页面(推荐)
2018/05/10 Javascript
10分钟彻底搞懂Http的强制缓存和协商缓存(小结)
2018/08/30 Javascript
vue移动端监听滚动条高度的实现方法
2018/09/03 Javascript
详解vue的双向绑定原理及实现
2019/05/05 Javascript
layui表格 返回的数据状态异常的解决方法
2019/09/10 Javascript
java和js实现的洗牌小程序
2019/09/30 Javascript
在vue中封装方法以及多处引用该方法详解
2020/08/14 Javascript
Jquery Fade用法详解
2020/11/06 jQuery
[49:05]Newbee vs TNC 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
Python读取ini文件、操作mysql、发送邮件实例
2015/01/01 Python
在DigitalOcean的服务器上部署flaskblog应用
2015/12/19 Python
Python判断文件或文件夹是否存在的三种方法
2017/07/27 Python
Python Unittest自动化单元测试框架详解
2018/04/04 Python
手机使用python操作图片文件(pydroid3)过程详解
2019/09/25 Python
sklearn-SVC实现与类参数详解
2019/12/10 Python
Python如何根据时间序列数据作图
2020/05/12 Python
Python select及selectors模块概念用法详解
2020/06/22 Python
python实现斗地主分牌洗牌
2020/06/22 Python
浅谈TensorFlow之稀疏张量表示
2020/06/30 Python
创业计划书如何吸引他人眼球
2014/01/10 职场文书
2014法制宣传日活动总结范文
2014/11/01 职场文书
2014年药店店长工作总结
2014/11/17 职场文书
零基础学java之方法的定义与调用详解
2022/04/10 Java/Android