使用Python 3从源代码中提取URL
我的问题是关于以下问题:使用Python 3从源代码中提取URL,python,html,python-3.x,html-parsing,Python,Html,Python 3.x,Html Parsing,我的问题是关于以下问题: 如果我不知道确切的URL,只是有一个应该出现在URL中的关键字怎么办?那么如何从页面源中提取url呢?尝试使用正则表达式 import re re.findall(r'(?i)href=["\']([^\s"\'<>]+)', content) 重新导入 re.findall(r'(?i)href=[“\'”]([^\s“\']+]),内容) 使用HTML解析器 在的情况下,可以将作为关键字参数值传递: from bs4 import Beautiful
如果我不知道确切的URL,只是有一个应该出现在URL中的关键字怎么办?那么如何从页面源中提取url呢?尝试使用正则表达式
import re
re.findall(r'(?i)href=["\']([^\s"\'<>]+)', content)
重新导入
re.findall(r'(?i)href=[“\'”]([^\s“\']+]),内容)
使用HTML解析器
在的情况下,可以将作为关键字参数值传递:
from bs4 import BeautifulSoup
word = "test"
data = "your HTML here"
soup = BeautifulSoup(data)
for a in soup.find_all('a', href=lambda x: x and word in x):
print(a['href'])
或者,a:
或者,使用:
嗯。。。将它们全部提取出来,然后依次检查。
import re
for a in soup.find_all('a', href=re.compile(word)):
print(a['href'])
for a in soup.select('a[href^="{word}"]'.format(word=word)):
print(a['href'])