python 正则获取url地址

sample:<a rpos="" cpos="title" href="http://xxx.com/a.html" style="font-family:Arial,SimSun,sans-serif;font-size:16px;color:#0000cc; text-decoration:none;" target="_blank">

我想获取http://xxx.com/a.html 请问如何写正则表达式

阅读 9.2k
3 个回答
/<a[^>]*href="([^"]+)"[^>]*>/

from bs4 import BeautifulSoup

soup = BeautifulSoup(sample)

url = soup.find('a').get('href')

这还不简单

text = '<a rpos="" cpos="title" href="http://xxx.com/a.html" style="font-family:Arial,SimSun,sans-serif;font-size:16px;color:#0000cc; text-decoration:none;" target="_blank">'

urlPattern = r'(href="http://[\s\S]+.html")'

pattern = re.compile(urlPattern)

match = re.findall(pattern,text,0)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题