如何使用Python正则表达式获取图像src?
如何使用正则表达式使用Python从以下html字符串获取图像的src如何使用Python正则表达式获取图像src?,python,html,regex,html-parsing,Python,Html,Regex,Html Parsing,如何使用正则表达式使用Python从以下html字符串获取图像的src 我试着用 matches = re.search('@src="([^"]+)"',text) print(matches[0]) 但是什么都没有只需在正则表达式中丢失@,它就可以工作了您可以稍微简化您的re: match = re.search(r'src="(.*?)"', text) 代替正则表达式,可以考虑使用: >>来自bs4导入组 >>>汤=美汤(垃圾) >>>soup.findAll('img')) []
我试着用
matches = re.search('@src="([^"]+)"',text)
print(matches[0])
但是什么都没有只需在正则表达式中丢失@,它就可以工作了您可以稍微简化您的
re
:
match = re.search(r'src="(.*?)"', text)
代替正则表达式,可以考虑使用:
>>来自bs4导入组
>>>汤=美汤(垃圾)
>>>soup.findAll('img'))
[]
>>>soup.findAll('img')[0]['src']
u'//nt3.ggpht.com/news/tbn/380jt5xHH6l_FM/6.jpg'
应该匹配的“@”字符是什么?您的输入字符串中没有这样的字符。正则表达式和html?的可能重复,Beautiful Soup会不会给解决方案增加很多开销img
标记相对容易解析(而且因为它们不包含其他文本,通常格式正确),所以它也会得到javascript文件。
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(junk)
>>> soup.findAll('img')
[<img src="//nt3.ggpht.com/news/tbn/380jt5xHH6l_FM/6.jpg" alt="" border="1" width="80" height="80" />]
>>> soup.findAll('img')[0]['src']
u'//nt3.ggpht.com/news/tbn/380jt5xHH6l_FM/6.jpg'