【发布时间】:2021-04-05 10:39:30
【问题描述】:
我想通过在href html 标记上使用正则表达式来抓取网站的外部链接和路径。
但我不知道是否有比我的代码更简单的方法:
import requests
import re
target_url = ("http://testphp.vulnweb.com/")
response = requests.get(target_url)
res = re.findall('href\=\"[\w.:/]+\"', response.content.decode("utf-8"))
for i in res:
patt = re.compile("\"[.:/\w]+\"")
not_raw = re.findall(patt, i)
raw = re.findall("[.:/\w]+", not_raw[0])
print(raw)
有没有办法,而不是使用正则表达式 3 次,从 href 标记中选择路径和链接而不捕获它?
我的意思是res 变量输出是这样的:
href="https://www.acunetix.com/vulnerability-scanner/"
我可以使用正则表达式来获取 res 变量中的 URL,如下所示?
https://www.acunetix.com/vulnerability-scanner/
【问题讨论】:
-
可能是因为通常不鼓励使用正则表达式抓取 HTML。使用 BeautifulSoup。
标签: python python-3.x regex web-scraping