|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import re
html = """<meta http-equiv = 'X-UA-Compab' content = 'IE = edge,chorme = 1'>
<meta http-equiv = 'content-type' content = 'text/html;chaset = utf-8'>
<meta content = 'always' name = 'referrer'>
<meta name = 'theme-color' content = '#2932el'>
<link rel = 'shortcut icon' href = '/favicon.ico' type = 'image/x-icon'/>
<link rel = 'icon' size = 'any' mask href = '//www.baidu.com/img/baidu.svg'>
<link rel = 'search' type = 'application/opensearchdescription+xml' href = '/content-search.xml'
title = '百度搜索'/>'"""
第一种情况:
pat = re.compile('href = "[0-9a-zA-Z\_\/\-\.]+"')
res = re.findall(pat,html)
res
运行结果:[]
第二种情况:
pat = re.compile("href = '[0-9a-zA-Z\_\/\-\.]+'")
res = re.findall(pat,html)
res
运行结果:["href = '/favicon.ico'",
"href = '//www.baidu.com/img/baidu.svg'",
"href = '/content-search.xml'"]
我的目的是获取html中href的内容
区别在re.compile中单双引号的使用,顺序不同为什么会出现不同的结果
因为你要匹配的内容,里面是单引号。引号不同是为了能够转义里面的单引号。
所以正则表达式是严格匹配的,所以里面必须得是单引号。
|
|