【发布时间】:2015-01-23 20:24:52
【问题描述】:
我想在文件中搜索与此模式匹配的数字:
<a href="test/?n=451484" >
然后获取号码451484:
我使用这种模式:
'
(test/?n=)
\d+
'
但这不起作用?
【问题讨论】:
标签: python html regex python-2.7 html-parsing
我想在文件中搜索与此模式匹配的数字:
<a href="test/?n=451484" >
然后获取号码451484:
我使用这种模式:
'
(test/?n=)
\d+
'
但这不起作用?
【问题讨论】:
标签: python html regex python-2.7 html-parsing
或者,您可以使用专门的工具:
BeautifulSoup)urlparse提取url参数值例子:
import re
from urlparse import urlparse, parse_qs
from bs4 import BeautifulSoup
data = """
<div>
<a href="test/?n=451484">link</a>
</div>
"""
soup = BeautifulSoup(data)
# filtering links with a specific "href" attribute value
link = soup.find('a', href=re.compile(r'test/\?n=\d+'))
url = link['href']
query = urlparse(url).query
print parse_qs(query)['n'][0] # prints 451484
【讨论】:
3 处变化
转义?
将d+ 括在括号中
在test\?n=周围放置括号
示例用法
>>> import re
>>> str='<a href="test/?n=451484" >'
>>> re.findall(r'test/\?n=(\d+)', str)
['451484']
【讨论】:
findall 时才需要,如我的示例所示。 findall 保存捕获组并打印它们,从而忽略匹配的test.. 并根据需要仅打印数字
要搜索文字 ? 字符,您需要使用 \ 对其进行转义。 ? 是正则表达式中的特殊字符,不能(通常)单独使用。
pattern = r"test/\?n=(\d+)"
【讨论】: