Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python 3从源代码中提取URL_Python_Html_Python 3.x_Html Parsing - Fatal编程技术网

使用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'])