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 实现随机数详解及实例代码
Apr 15 Python
关于Python中浮点数精度处理的技巧总结
Aug 10 Python
python numpy 显示图像阵列的实例
Jul 02 Python
Django框架多表查询实例分析
Jul 04 Python
python环形单链表的约瑟夫问题详解
Sep 27 Python
Python 从相对路径下import的方法
Dec 04 Python
python pands实现execl转csv 并修改csv指定列的方法
Dec 12 Python
详解Python3序列赋值、序列解包
May 14 Python
浅谈Python3实现两个矩形的交并比(IoU)
Jan 18 Python
python如何提取英语pdf内容并翻译
Mar 03 Python
python多进程 主进程和子进程间共享和不共享全局变量实例
Apr 25 Python
浅谈Python xlwings 读取Excel文件的正确姿势
Feb 26 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 方便水印和缩略图的图形类
2009/05/21 PHP
DW中链接mysql数据库时,建立字符集中文出现乱码的解决方法
2010/03/27 PHP
深入array multisort排序原理的详解
2013/06/18 PHP
浅析SVN常见问题及解决方法
2013/06/21 PHP
thinkphp中的url跳转用法分析
2016/07/12 PHP
php实现的pdo公共类定义与用法示例
2017/07/19 PHP
兼容FireFox 的 js 日历 支持时间的获取
2009/03/04 Javascript
jquery 最简单的属性菜单
2009/10/08 Javascript
从零开始学习jQuery (四) jQuery中操作元素的属性与样式
2011/02/23 Javascript
模仿百度三维地图的js数据分享
2011/05/12 Javascript
Javascript中arguments对象详解
2014/10/22 Javascript
基于BootStrap Metronic开发框架经验小结【一】框架总览及菜单模块的处理
2016/05/12 Javascript
Bootstrap Table使用整理(一)
2017/06/09 Javascript
Vue.js分页组件实现:diVuePagination的使用详解
2018/01/10 Javascript
jquery获取元素到屏幕四周可视距离的方法
2018/09/05 jQuery
Node.js中的不安全跳转如何防御详解
2018/10/21 Javascript
JavaScript实现的九种排序算法
2019/03/04 Javascript
bootstrap table插件动态加载表头
2019/07/19 Javascript
Node.js API详解之 Error模块用法实例分析
2020/05/14 Javascript
vuex的数据渲染与修改浅析
2020/11/26 Vue.js
Python入门_学会创建并调用函数的方法
2017/05/16 Python
对Python中的条件判断、循环以及循环的终止方法详解
2019/02/08 Python
使用matplotlib中scatter方法画散点图
2019/03/19 Python
python处理大日志文件
2019/07/23 Python
完美解决keras保存好的model不能成功加载问题
2020/06/11 Python
基于Python爬取搜狐证券股票过程解析
2020/11/18 Python
Selenium 配置启动项参数的方法
2020/12/04 Python
顶丰TOPPIK台湾官网:增发纤维假发,告别秃发困扰
2018/06/13 全球购物
土耳其玩具商店:Toyzz Shop
2019/08/02 全球购物
中班下学期幼儿评语
2014/12/30 职场文书
北京故宫导游词
2015/01/31 职场文书
2015社区精神文明建设工作总结
2015/04/21 职场文书
2016年学习雷锋精神广播稿
2015/12/17 职场文书
导游词之吉林吉塔
2019/11/11 职场文书
Apache SeaTunnel实现 非CDC数据抽取
2022/05/20 Servers
在ubuntu下安装go开发环境的全过程
2022/08/05 Golang