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
0
投稿
猜你喜欢
- 本文实例为大家分享了python创建tcp服务端和客户端的具体代码,供大家参考,具体内容如下1.服务端serverfrom socket i
- vue + iview 实现一个手机分段的提示框,知识点还没总结,供大家参考,具体内容如下<template> &l
- 最近遇到了一个下载静态html报表的需求,需要以提供压缩包的形式完成下载功能,实现的过程中发现相关文档非常杂,故总结一下自己的实现。开发环境
- 说明:通过随机产生密码,然后将密码EMail给注册用户,你可以确认用户的EMail填写是否正确。自动产生的密码往往安全性更高,同时,你可以过
- 问题:我们每天都要编写一些Python程序,或者用来处理一些文本,或者是做一些系统管理工作。程序写好后,只需要敲下python命令,便可将程
- 后台数据库: [Microsoft Access] 与 [Microsoft Sql Server] 更换之后,ASP代码应注意要修改的一些
- 修改密码://选择数据库use mysql;//修改密码update user set password=password('新密码
- Python-opencv+KNN求解数独最近一直在玩数独,突发奇想实现图像识别求解数独,输入到输出平均需要0.5s。整体思路大概就是识别出
- 本文实例讲述了Python3搜索及替换文件中文本的方法。分享给大家供大家参考。具体实现方法如下:# 将文件中的某个字符串改变成另一个 # 下
- 介绍与创建型模式类似,工厂模式创建对象(视为工厂里的产品)时无需指定创建对象的具体类。工厂模式定义一个用于创建对象的接口,这个接口由子类决定
- 下面,我们就从当前时间来取得随机数,调用的时候用包含文件就可以了:<!--#INCLUDE VIRTUAL="/q
- Numpy提供了几种数据保存的方法。以3*4数组a为例:1. a.tofile("filename.bin")这种方法只
- 本文实例讲述了Python pandas RFM模型应用。分享给大家供大家参考,具体如下:什么是RFM模型根据美国数据库营销研究所Arthu
- 场景现在的项目,基本都是前后端分离,后端只要提供Json等格式的数据就行。在这个背景下,模板渲染这个功能备受冷落,很少会在项目中用到。虽然在
- 目录1. 折线图概述1.1什么是折线图?1.2折线图使用场景1.3绘制折线图步骤1.4案例展示2. 折线2D属性2.1linestyle:折
- 我是以Python开门的,我还是觉得Python也可以进行地形三维可视化,当然这里需要借助第三方库,so,我就来介绍:Python一个很重要
- 本文为大家分享了MySQL 5.7版本的安装使用详细教程,更改数据库data的存储路径,供大家参考,具体内容如下因为看到mysql5.7加入
- 问题背景目前的linux发行版上,有很多安装了两个版本的python。我的机器上默认的版本为python 2.x。且在使用easy_inst
- 一般iis中比较简单,iis6如下图所示即可:很多购买虚拟主机空间的用户,如果空间商提供了在线管理程序,也可以实现。具体的看下帮助即可。需要
- 前言在爬虫系列文章 优雅的HTTP库requests 中介绍了 requests 的使用方式,这一次我们用 requests 构建一个知乎