python re正则匹配网页中图片url地址的方法
作者:Arckal 发布时间:2023-06-15 20:44:20
标签:python,re,url
最近写了个python抓取必应搜索首页http://cn.bing.com/的背景图片并将此图片更换为我的电脑桌面的程序,在正则匹配图片url时遇到了匹配失败问题。
要抓取的图片地址如图所示:
首先,使用这个pattern
reg = re.compile('.*g_img={url: "(http.*?jpg)"')
无论怎么匹配都匹配不到,后来把网页源码抓下来放在notepad++中查看,并用notepad++的正则匹配查找,很轻易就匹配到了,如图:
后来我写了个测试代码,把图片地址在的那一行保存在一个字符串中,很快就匹配到了,如下面代码所示,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)
即可完美解决问题。
来源:https://blog.csdn.net/u014108439/article/details/52982461
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 废话少说,先上代码File:logger.conf[formatters]keys=default[formatter_default]fo
- 在读文件时常常得到一些\n和引号之类的符号,可以使用字符串的成员函数strip()来去除。1.去除首尾不需要的字符a= '"
- 写在前面原计划继续写一篇Portia的使用博客,结果在编写代码途中发现,在windows7的DockerToolbox里面使用Portia错
- 计算分页,嘿嘿一次搞定不用判断intNumPage = Abs(Int(-(intNumRecord/intPerPage)))  
- # 0. PyCharm 常用快捷键# 1. 查看使用库源码PyCharm 主程序员在 Stackoverflow 上答道经常听人说,多看源
- Python编程中raise可以实现报出错误的功能,而报错的条件可以由程序员自己去定制。在面向对象编程中,可以先预留一个方法接口不实现,在其
- 注:我指一个网站被第三方网站以iframe的形式调用时,被调用网站的禁止策略 和 调用网站的突破禁止策略,跟XSS麽关系,但跟clickja
- 本文实例讲述了Python使用matplotlib简单绘图。分享给大家供大家参考,具体如下:# -*- coding:utf-8 -*-#!
- 前言喜马拉雅是专业的音频分享平台,汇集了有声小说,有声读物,有声书,FM电台,儿童睡前故事,相声小品,鬼故事等数亿条音频,我最喜欢听民间故事
- 本文根据自己初学经验编写的使用xlwt模块设置单元格的一些基本样式,如设置单元格的背景颜色,下框线,字体,字体的颜色,设置列宽行高,插入简单
- 本文实例讲述了PHP函数shuffle()取数组若干个随机元素的方法。分享给大家供大家参考,具体如下:有时候我们需要取数组中若干个随机元素(
- 在本文中,作者介绍了 5 种方法,也许在入门阶段时,我们还不太了解它们,但在实战中这 5 个技巧非常实用。字符串运算字符串本质上也是一种元组
- Linux 自动备份oracle数据:曾经有个同事,来回操作开发和生产的数据库,结果误删了生产的数据库,那种心情我想不是一般人能理解的,虽然
- 业务需求:需要测试手机滑动解锁失败时事件的次数及等待的时间,本来想利用Python+Appium实现,但是Appium运行时自动给我解锁了.
- laravel入门简介作为PHP最常用的框架之一,Laravel的框架目录布置得尤其清晰,适用于各种类型的项目开发。今天来记录下larave
- 什么是字符串格式化,为什么需要这样做?我们有时候刷抖音/B站看到封面很好看,但是进入直播发现,不过如此!想必主播通过某种方式把输出转换为读者
- 数据库优化是一项很复杂的工作,因为这最终需要对系统优化的很好理解才行。尽管对系统或应用系统的了解不多的情况下优化效果还不错,但是如果想优化的
- 演示:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//
- pandas DataFrame是二维的,所以,它既有列索引,又有行索引上一篇里只介绍了列索引:import pandas as pddf
- 队列和优先队列(Priority Queue)队列是一种可以完成插入和删除的数据结构。普通队列是先进先出(FIFO), 即先插入的先被删除。