Python爬虫正则表达式匹配多个给定字符串间的内容
我在编写Python爬虫时,有如下字符串:a=str(<html><body><p>[<ahref="/aero-desktop-wallpapers.html"titl...
我在编写Python爬虫时,有如下字符串:a = str(
<html><body><p>[<a href="/aero-desktop-wallpapers.html" title="Aero HD Wallpapers">Aero</a>, <a href="/animals-desktop-wallpapers.html" title="Animals HD Wallpapers">Animals</a>, <a href="/architecture-desktop-wallpapers.html" title="Architecture HD Wallpapers">Architecture</a>, Wallpapers">Artistic</a>, ........(省略)......... <a href="/vintage-desktop-wallpapers.html" title="Vintage HD Wallpapers">Vintage</a>]</p></body></html>
用正则表达式re.findall('.*<a href="/(.*)-desktop-wallpapers.html.*',str(a))匹配时,只返回最后一个结果'Vintage',请问如何匹配出'Aero','Animals',.......'vintage',构成的列表?
描述可能很模糊,在此道歉,希望有人能给予帮助! 展开
<html><body><p>[<a href="/aero-desktop-wallpapers.html" title="Aero HD Wallpapers">Aero</a>, <a href="/animals-desktop-wallpapers.html" title="Animals HD Wallpapers">Animals</a>, <a href="/architecture-desktop-wallpapers.html" title="Architecture HD Wallpapers">Architecture</a>, Wallpapers">Artistic</a>, ........(省略)......... <a href="/vintage-desktop-wallpapers.html" title="Vintage HD Wallpapers">Vintage</a>]</p></body></html>
用正则表达式re.findall('.*<a href="/(.*)-desktop-wallpapers.html.*',str(a))匹配时,只返回最后一个结果'Vintage',请问如何匹配出'Aero','Animals',.......'vintage',构成的列表?
描述可能很模糊,在此道歉,希望有人能给予帮助! 展开
1个回答
展开全部
你的正则表达式使用了贪婪模式的匹配(.*),应该用非贪婪模式,正则表达式应该为<a href="/(.*?)-desktop-wallpapers.html
完整的python语言程序如下
#!/usr/bin/python3
import re
a = '<html><body><p>[<a href="/aero-desktop-wallpapers.html" title="Aero HD Wallpapers">Aero</a>, <a href="/animals-desktop-wallpapers.html" title="Animals HD Wallpapers">Animals</a>, <a href="/architecture-desktop-wallpapers.html" title="Architecture HD Wallpapers">Architecture</a>,Wallpapers">Artistic</a>, ........(省略)......... <a href="/vintage-desktop-wallpapers.html" title="Vintage HD Wallpapers">Vintage</a>]</p></body></html>'
titles = re.findall('<a href="/(.*?)-desktop-wallpapers.html',str(a))
print (titles)
运行结果
['aero', 'animals', 'architecture', 'vintage']
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询