【问题标题】:how to put if condition in python bs4如何在python bs4中放置if条件
【发布时间】:2021-04-12 08:55:07
【问题描述】:

当我从网站上抓取内容时,一些链接点在 src 标记中有 HTTP,为此我添加了此代码

from bs4 import BeautifulSoup


html = """
<div class="answer-given-body ugc-base">
  <p><img alt="" src="//d2vlcm61l7u1fs.cloudfront.net/media%2F61d%2F61d6042d-e4dd-41d9-9a5c-0ceb481ddbc9%2FphpKFGb9B.png"/><img alt="" src="//d2vlcm61l7u1fs.cloudfront.net/media%2Fd72%2Fd72dfa6c-8e50-475a-86cf-678a04ae4606%2FphpQZYPYo.png"/><img alt="" src="//d2vlcm61l7u1fs.cloudfront.net/media%2F4c7%2F4c775a01-8590-4b93-bc20-03d282586f95%2FphpE7XFWI.png"/></p>
  </div>
"""

soup = BeautifulSoup(html, "html.parser")

# Select all the `img` tags
for tag in soup.select(".answer-given-body.ugc-base img"):
    tag["src"] = "https:" + tag["src"]

print(soup.prettify())

但是有些链接有 HTTP: 在 src 然后这段代码也再次添加 HTTP 到那个链接看这个:

from bs4 import BeautifulSoup


html = """
<div class="answer-given-body ugc-base">
  <p><img alt="" src="//d2vlcm61l7u1fs.cloudfront.net/media%2F61d%2F61d6042d-e4dd-41d9-9a5c-0ceb481ddbc9%2FphpKFGb9B.png"/><img alt="" src="https://d2vlcm61l7u1fs.cloudfront.net/media%2Fd72%2Fd72dfa6c-8e50-475a-86cf-678a04ae4606%2FphpQZYPYo.png"/><img alt="" src="//d2vlcm61l7u1fs.cloudfront.net/media%2F4c7%2F4c775a01-8590-4b93-bc20-03d282586f95%2FphpE7XFWI.png"/></p>
  </div>
"""

soup = BeautifulSoup(html, "html.parser")

# Select all the `img` tags
for tag in soup.select(".answer-given-body.ugc-base img"):
    tag["src"] = "https:" + tag["src"]

print(soup.prettify())

所以我需要把 if 条件放在那里,但我不知道如何添加请帮助我之前谢谢

【问题讨论】:

  • if 'http' in string: 这行得通吗?

标签: python html beautifulsoup python-requests python-requests-html


【解决方案1】:

我不知道 BeautifulSoup 内置的解决方案,但您可以简单地以找到的 URL 为条件。

您可能需要注意的另一个地方是本地引用的图像,这些图像以/ 开头,或者只是将其名称“image.png”作为 src。在这些情况下,您需要附加页面的源 URL。

from bs4 import BeautifulSoup


html = """
<div class="answer-given-body ugc-base">
  <p><img alt="" src="//d2vlcm61l7u1fs.cloudfront.net/media%2F61d%2F61d6042d-e4dd-41d9-9a5c-0ceb481ddbc9%2FphpKFGb9B.png"/><img alt="" src="https://d2vlcm61l7u1fs.cloudfront.net/media%2Fd72%2Fd72dfa6c-8e50-475a-86cf-678a04ae4606%2FphpQZYPYo.png"/><img alt="" src="//d2vlcm61l7u1fs.cloudfront.net/media%2F4c7%2F4c775a01-8590-4b93-bc20-03d282586f95%2FphpE7XFWI.png"/></p>
  </div>
"""

soup = BeautifulSoup(html, "html.parser")

# Select all the `img` tags
for tag in soup.select(".answer-given-body.ugc-base img"):
    tag["src"] = tag["src"] if tag["src"].startswith("http") else "https:" + tag["src"]

print(soup.prettify())

处理源的一种建议方法是使用 urllib.parse 函数: from urllib.parse import urlparse 正如 here 解释的那样,它将 url 分解为它的组件,然后您可以使用这些组件重新添加这些组件以进行请求:

>>> from urllib.parse import urlparse
>>> urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> urlparse('www.cwi.nl/%7Eguido/Python.html')
ParseResult(scheme='', netloc='', path='www.cwi.nl/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> urlparse('help/Python.html')
ParseResult(scheme='', netloc='', path='help/Python.html', params='',
            query='', fragment='')

【讨论】:

    猜你喜欢
    • 2011-03-04
    • 2020-01-09
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多