Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.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
如何从HTML中提取链接(使用python)_Python_Html_Parsing - Fatal编程技术网

如何从HTML中提取链接(使用python)

如何从HTML中提取链接(使用python),python,html,parsing,Python,Html,Parsing,所以我下载了一个网页的HTML。我应该从HTML中提取所有链接并输出它们。这是我的密码 f = open('html.py','r') heb = f.readlines() arry = [] if 'href' in heb: arry = arry.append(href) print(arry) 我试图列出一个链接列表并输出它,但老实说,我真的迷路了。有人能给我指出正确的方向吗?我在想regex可能是最好的选择 谢谢您可以使用Beautiful Soup(您需要安装它

所以我下载了一个网页的HTML。我应该从HTML中提取所有链接并输出它们。这是我的密码

f = open('html.py','r')
heb = f.readlines()
arry = []
if 'href' in heb:
    arry = arry.append(href)

    print(arry)
我试图列出一个链接列表并输出它,但老实说,我真的迷路了。有人能给我指出正确的方向吗?我在想regex可能是最好的选择
谢谢

您可以使用Beautiful Soup(您需要安装它,例如使用
pip安装BeautifulSoup4
):


您可以使用Beautiful Soup(您需要安装它,例如使用
pip安装Beautiful Soup4
):


不要在html上使用正则表达式!使用类似BeautifulSoup的HTML解析器。在HTML上可能重复Do't Use regex!使用类似BeautifulSoup的HTML解析器。的可能重复项
import bs4

with open("my-file.html") as f:
    soup = bs4.BeautifulSoup(f)

links = [link['href'] for link in soup('a') if 'href' in link.attrs]