在html中查找所有链接

2024-09-30 08:25:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python中使用beauthoulsoup。在报废页面中,链接不包含在<a href>标记中。在

我想使用soup操作获取所有以http/https开头的链接。我尝试过一些regex给定的here,但它们给了我意想不到的结果。 所以我想如果有什么可以用汤吗?在

我想从中获取链接的示例响应:

<html>\n<head>\n</head>\n<link href="https://fonts.googleapis.com/css?family=Open+Sans:600" rel="stylesheet"/>\n<style>\n    html, body {\n    height: 100%;\n    width: 100%;\n    }\n\n    body {\n    background: #F5F6F8;\n    font-size: 16px;\n    font-family: \'Open Sans\', sans-serif;\n    color: #2C3E51;\n    }\n    .main {\n    display: flex;\n    align-items: center;\n    justify-content: center;\n    height: 100vh;\n    }\n    .main > div > div,\n    .main > div > span {\n    text-align: center;\n    }\n    .main span {\n    display: block;\n    padding: 80px 0 170px;\n    font-size: 3rem;\n    }\n    .main .app img {\n    width: 400px;\n    }\n  </style>\n<script type="text/javascript">\n      var fallback_url = "null";\n      var store_link = "itms-apps://itunes.apple.com/GB/app/id1032680895?ls=1&mt=8";\n      var web_store_link = "https://itunes.apple.com/GB/app/id1032680895?mt=8";\n      var loc = window.location;\n      function redirect_to_web_store(loc) {\n        loc.href = web_store_link;\n      }\n      function redirect(loc) {\n        loc.href = store_link;\n        if (fallback_url.startsWith("http")) {\n          setTimeout(function() {\n            loc.href = fallback_url;\n          },5000);\n        }\n      }\n  </script>\n<body onload="redirect(loc)">\n<div class="main">\n<div class="workarea">\n<div class="logo">\n<img onclick="redirect_to_web_store(loc)" src="https://cdnappicons.appsflyer.com/app|id1032680895.png" style="width:200px;height:200px;border-radius:20px;"/>\n</div>\n<span>BetBull: Sports Betting &amp; Tips</span>\n<div class="app">\n<img onclick="redirect_to_web_store(loc)" src="https://cdn.appsflyer.com/af-statics/images/rta/app_store_badge.png"/>\n</div>\n</div>\n</div>\n</body>\n</html>

尝试过:

^{pr2}$

结果:

['https://fonts.googleapis.com/css?family=Open', '//itunes.apple.com/GB/app/id1032680895?ls=1', 'https://itunes.apple.com/GB/app/id1032680895?mt=8', 'window.location', 'loc.href', 'loc.href', 'fallback_url.startsWith', 'loc.href', 'https://cdnappicons.appsflyer.com/app', 'id1032680895.png', 'https://cdn.appsflyer.com/af-statics/images/rta/app_store_badge.png']

正如您在上面看到的,我不确定为什么regex要匹配甚至不是url的东西。在

What I have tried. 最赞成和接受的答案是这里根本无法检测链接!! 我不知道我做错了什么


Tags: storehttpsdivcomwebappurl链接
1条回答
网友
1楼 · 发布于 2024-09-30 08:25:39

问题在于,您将协议设为可选的,并且如果引擎对其余模式满意,则不会强制匹配该协议。试试这个:

(?:(?:https?|ftp):\/\/|\bwww\.)[^\s"']+

不是防弹的,但好多了。它匹配以https?ftp开头的字符串,或者那些没有协议但www.的字符串

观看直播demo here

相关问题 更多 >

    热门问题