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应用程序在windows下不出现cmd窗口的办法
May 29 Python
Python实现的监测服务器硬盘使用率脚本分享
Nov 07 Python
Python中datetime常用时间处理方法
Jun 15 Python
Python实现的科学计算器功能示例
Aug 04 Python
Python 模拟员工信息数据库操作的实例
Oct 23 Python
python判断字符串是否是json格式方法分享
Nov 07 Python
python微信跳一跳系列之色块轮廓定位棋盘
Feb 26 Python
Django异步任务之Celery的基本使用
Mar 23 Python
十个Python练手的实战项目,学会这些Python就基本没问题了(推荐)
Apr 26 Python
python查看数据类型的方法
Oct 12 Python
python机器学习库xgboost的使用
Jan 20 Python
浅谈JupyterNotebook导出pdf解决中文的问题
Apr 22 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+dojo 的数据库保存拖动布局的一个方法dojo 这里下载
2007/03/07 PHP
浏览器关闭后,能继续执行的php函数(ignore_user_abort)
2012/08/01 PHP
PHP时间戳与日期之间转换的实例介绍
2013/04/19 PHP
PHP ob缓存以及ob函数原理实例解析
2020/11/13 PHP
windows系统php环境安装swoole具体步骤
2021/03/04 PHP
javascript通过navigator.userAgent识别各种浏览器
2013/10/25 Javascript
jquery日历控件实现方法分享
2014/03/07 Javascript
28个常用JavaScript方法集锦
2015/01/14 Javascript
学习javascript面向对象 理解javascript原型和原型链
2016/01/04 Javascript
js倒计时显示实例
2016/12/11 Javascript
JS去除字符串中空格的方法
2017/02/14 Javascript
Angular 4根据组件名称动态创建出组件的方法教程
2017/11/01 Javascript
Webstorm2016使用技巧(SVN插件使用)
2018/10/29 Javascript
bootstrap table实现横向合并与纵向合并
2019/07/18 Javascript
微信小程序3D轮播实现代码
2019/09/19 Javascript
基于JS实现简单滑块拼图游戏
2019/10/12 Javascript
JavaScript实现轮播图特效
2020/04/10 Javascript
jquery实现简易验证插件封装
2020/09/13 jQuery
vue实现简单计算商品价格
2020/09/14 Javascript
Python实现的Kmeans++算法实例
2014/04/26 Python
Python实现遍历windows所有窗口并输出窗口标题的方法
2015/03/13 Python
Hadoop中的Python框架的使用指南
2015/04/22 Python
Python实现爬取逐浪小说的方法
2015/07/07 Python
Python实现查找系统盘中需要找的字符
2015/07/14 Python
Python正则表达式完全指南
2017/05/25 Python
python Tkinter的图片刷新实例
2019/06/14 Python
Pytorch之parameters的使用
2019/12/31 Python
Django修改app名称和数据表迁移方案实现
2020/09/17 Python
python爬虫看看虎牙女主播中谁最“顶”步骤详解
2020/12/01 Python
css3 按钮样式简单可扩展创建
2013/03/18 HTML / CSS
详解HTML5 LocalStorage 本地存储
2016/12/23 HTML / CSS
Boutique 1美国:阿联酋奢侈时尚零售商
2017/10/16 全球购物
会计电算化专业应届大学生求职信
2013/10/22 职场文书
《大自然的语言》教学反思
2014/04/08 职场文书
2015年销售助理工作总结
2015/05/11 职场文书
Win11 22H2 2022怎么更新? 获得Win1122H22022版本升级技巧
2022/09/23 数码科技