如何从没有 ancore 标签的 HTML 中获取所有链接?

how to fetch all links from html without ancore tag?

提问人:zircon 提问时间:4/10/2023 最后编辑:Wiktor Stribiżewzircon 更新时间:4/12/2023 访问量:41

问:

我想从代码中给出的链接中获取所有链接,尤其是这个 https://api.somthing.com/v1/companies/ 链接。我在网上找到的所有正则表达式都只获取简单的链接,例如 https://api.somthing.com

import requests
import re
from bs4 import BeautifulSoup

url='https://www.linkdin.com/'

x = requests.get(url)
html_doc=x.text
soup = BeautifulSoup(html_doc,"html.parser" )
print(soup)

python beautifulsoup html 解析

评论


答:

1赞 Timeless 4/10/2023 #1

您可以直接从响应内容中找到所有网址:

p= r'https://api\.something\.com/.*?(?=")'

urls = re.findall(p, html_doc)

​ 输出:

['https://api.something.com/v1/companies/postings/733260034',
 'https://api.something.com/v1/companies/postings/371262356',
 'https://api.something.com/v1/companies/postings/465637233',
 'https://api.something.com/v1/companies/postings/315747724,
...

评论

0赞 Timeless 4/10/2023
这不是您在问题/帖子中要求的。你能更具体一点吗?
0赞 zircon 4/10/2023
实际上,我想要一个可以获取任何类型的 url 的 regx,而不是任何特定类型的url.@Timeless
1赞 Timeless 4/10/2023
在这种情况下,请使用p = r'https://.+?\.com/.*?(?=")'
0赞 markalex 4/10/2023
如果我们假设链接包含在属性中,我相信你的前瞻应该是 .(?="|')