【问题标题】:find all links in html parsed beautiful soup在 html 中找到所有链接解析美丽的汤
【发布时间】:2018-11-03 15:31:19
【问题描述】:

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

我想使用汤操作获取所有以 http/https 开头的链接。我尝试了一些给定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>

试过了:

regex_pattern_to_find_all_links = r'(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+'
soup = BeautifulSoup(resp.read(), 'html.parser')
urls = re.findall(regex_pattern_to_find_all_links, str(soup))

结果:

['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']

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

What I have tried. 这里最受欢迎和接受的答案根本无法检测到链接! 我不确定我做错了什么,

【问题讨论】:

  • 您将协议设为可选。所以这就是原因。
  • 好的,我如何让它成为必要的?
  • 作为第一部分,请改用 (?:(?:https?|ftp):\/\/|\bwww\.)
  • 在这里查看演示 regex101.com/r/Sz7p1M/1
  • 它似乎对你有用,所以我将它作为答案发布在下面。

标签: python regex web-scraping beautifulsoup


【解决方案1】:

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

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

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

观看直播demo here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 2022-10-20
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    相关资源
    最近更新 更多