【问题标题】:Tried to extract urls by using soup.select and soup.find_all尝试使用soup.select 和soup.find_all 提取url
【发布时间】:2017-10-13 06:49:10
【问题描述】:

这是网页 HTML 源代码的一部分:

<a href="http://www.abcde.com"> <img style="width:100%" src="/FileUploads/B/763846f.jpg" alt="search" title="search" /></a>
<a id="parts_img01" href="/Result?s=9&amp;type=%E4&amp;name=%E9"><h4 style=""><i class="fa f-c" aria-hidden="true"></i>apple</h4></a>
<a id="parts_img02" href="/Result?s=12&amp;type=%E4&amp;name=%E4"><h4 style=""><i class="fa f-c" aria-hidden="true"></i>banana</h4></a>
<a id="parts_img03" href="/Result?s=10&amp;type=%E4&amp;name=%E8"><h4 style=""><i class="fa f-c" aria-hidden="true"></i>cherry</h4></a>
<a id="parts_img07" href="/Result?s=14&amp;type=%E4&amp;name=%E8"><h4 style=""><i class="fa f-c" aria-hidden="true"></i>melon</h4></a>

我想提取我想要的网址,比如以 /Result 开头的网址? 我刚刚了解到您可以在美丽的汤中使用soup.find_all 和soup.select。

soup.find_all:

icon = soup.find_all(id = re.compile("parts_img"))

其中一个结果将成功打印:

<a href="/Result?s=9&amp;type=%E4&amp;name=%E9" id="parts_img01"><h4 style=""><i aria-hidden="true" class="fa f-c"></i>apple</h4></a>

汤选择:

for item in soup.select(".fa f-c"):
    print(item['href'])

这行不通……

有没有一种方法可以直接从 html 中提取 url? 我只想打印:

/Result?s=9&amp;type=%E4&amp;name=%E9
/Result?s=12&amp;type=%E4&amp;name=%E4
/Result?s=10&amp;type=%E4&amp;name=%E8
/Result?s=14&amp;type=%E4&amp;name=%E8

【问题讨论】:

    标签: python html web-scraping beautifulsoup


    【解决方案1】:

    要在不使用正则表达式的情况下获得相同的输出:

    html = """
     <a href="http://www.abcde.com"> <img style="width:100%" src="/FileUploads/B/763846f.jpg" alt="search" title="search" /></a>
    <a id="parts_img01" href="/Result?s=9&amp;type=%E4&amp;name=%E9"><h4 style=""><i class="fa f-c" aria-hidden="true"></i>apple</h4></a>
    <a id="parts_img02" href="/Result?s=12&amp;type=%E4&amp;name=%E4"><h4 style=""><i class="fa f-c" aria-hidden="true"></i>banana</h4></a>
    <a id="parts_img03" href="/Result?s=10&amp;type=%E4&amp;name=%E8"><h4 style=""><i class="fa f-c" aria-hidden="true"></i>cherry</h4></a>
    <a id="parts_img07" href="/Result?s=14&amp;type=%E4&amp;name=%E8"><h4 style=""><i class="fa f-c" aria-hidden="true"></i>melon</h4></a>
    """
    from bs4 import BeautifulSoup
    
    soup = BeautifulSoup(html, "lxml")
    for link in soup.select("[id^='parts_img']"):
        print(link['href'])
    

    结果:

    /Result?s=9&type=%E4&name=%E9
    /Result?s=12&type=%E4&name=%E4
    /Result?s=10&type=%E4&name=%E8
    /Result?s=14&type=%E4&name=%E8
    

    【讨论】:

      【解决方案2】:

      我认为这段代码将说明从给定的 html 中提取 href

       html = """<a href="http://www.abcde.com"> <img style="width:100%" src="/FileUploads/B/763846f.jpg" alt="search" title="search" /></a>
      <a id="parts_img01" href="/Result?s=9&amp;type=%E4&amp;name=%E9"><h4 style=""><i class="fa f-c" aria-hidden="true"></i>apple</h4></a>
      <a id="parts_img02" href="/Result?s=12&amp;type=%E4&amp;name=%E4"><h4 style=""><i class="fa f-c" aria-hidden="true"></i>banana</h4></a>
      <a id="parts_img03" href="/Result?s=10&amp;type=%E4&amp;name=%E8"><h4 style=""><i class="fa f-c" aria-hidden="true"></i>cherry</h4></a>
      <a id="parts_img07" href="/Result?s=14&amp;type=%E4&amp;name=%E8"><h4 style=""><i class="fa f-c" aria-hidden="true"></i>melon</h4></a>"""
      from bs4 import BeautifulSoup as Soup
      import re
      from urllib.parse import urljoin
      parser = Soup(html, "lxml")
      href = [ urljoin("http://www.abcde.com", a["href"]) for a in parser.findAll("a", {"id" : re.compile('parts_img.*')})]
      print(href)
      

      【讨论】:

      • 它正在工作!谢谢!我还有一个问题,在这种情况下,网址不完整。我需要使用所有 url 加入 www.abcde.com,但由于 href 是一个列表,它会出现这样的错误:Cannot mix str and non-str arguments。顺便说一句,我使用 urljoin。
      • 请在原始答案中找到使用 urllib.parse 库将相对 url 加入绝对
      【解决方案3】:

      我正在使用

      #!/usr/bin/python
      
      import requests
      from bs4 import BeautifulSoup
      import re
      
      top_url = 'https://a-certain.org/item-index'
      response = requests.get(top_url)
      html = response.content
      soup = BeautifulSoup(html, 'html.parser')
      items = soup.select('a[href^="http://a-certain.org/items"]')
      for item in items:
              print(items['href'])
      

      输出是

      http://a-certain.org/items/item1/
      http://a-certain.org/items/item2/
      http://a-certain.org/items/item3/
      

      【讨论】:

        猜你喜欢
        • 2017-12-13
        • 2016-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-31
        • 1970-01-01
        • 2022-11-30
        相关资源
        最近更新 更多