【问题标题】:How to identify which HTML tags or classes to specify when scraping from a webpage?从网页抓取时如何识别要指定的 HTML 标记或类?
【发布时间】:2018-05-23 15:55:04
【问题描述】:

我想抓取网站中的新闻链接(在下面的屏幕截图中突出显示):

当我inspect页面时,我看到我想要的链接包含在col-sm-5标签h5下的类中。我想在那个 div 类 col-sm-5 中抓取所有 4 个链接(带有标签 li)。因此,我编写了以下代码来提取链接:

import requests 
page = requests.get("http://www3.asiainsurancereview.com/News","html.parser")
soup = BeautifulSoup(page.text, "html.parser")
li_box = soup.find('h5', attrs={'class': 'col_sm_5'})
print(li_box) 

但是我得到的输出是none;我想它找不到标签。那么,我的问题是,如何指定查找和提取链接所需的类、标签或其他信息?

【问题讨论】:

    标签: python html web-scraping beautifulsoup


    【解决方案1】:

    您正在尝试访问页面 HTML 中不存在的元素。

    li_box = soup.find('h5', attrs={'class': 'col_sm_5'})
    

    在这一行中,您正在尝试获取带有“col_sm_5”类的 h5 标记,该标记在页面的 HTML 中不存在。在 HTML 中,只有类 'col-sm-5' 的 'div' 退出。

    现在解决方案。最简单的方法是使用 beautifulSoup 的 select()。

    >>> page = requests.get("http://www3.asiainsurancereview.com/News","html.parser")
    >>> soup = BeautifulSoup(page.content, "html.parser")
    >>> aa = soup.select("div.col-sm-5 ul.list-default li h5 a")
    >>> for a in aa:
    ...     print(a.attrs['href'])
    ...
    /Mock-News-Article/id/42945/Type/eDaily/New-Zealand-Govt-starts-public-consultation-phase-of-review-of-insurance-law
    /Mock-News-Article/id/42946/Type/eDaily/India-M-A-deals-brewing-in-insurance-sector
    /Mock-News-Article/id/42947/Type/eDaily/China-Online-insurance-premiums-soar-31-in-1Q2018
    /Mock-News-Article/id/42948/Type/eDaily/South-Korea-Courts-increasingly-see- 65-as-retirement-age
    >>>
    

    soup.select 将在 div 内的 li 内的 h5 内查找所有 a 标签,类 col-sm-5

    然后遍历所有元素并获得所需的 attr,在您的情况下是 href。

    【讨论】:

      【解决方案2】:

      requests.get() 不需要"html.parser",这是用于beautifulsoup 的。

      另外,类名是col-sm-5 而不是col_sm_5

      最好使用响应content 而不是text。 (也许不是真的,见 cmets)

      你可以像下面这样使用 css 选择器:

      import requests
      from bs4 import BeautifulSoup
      
      page = requests.get("http://www3.asiainsurancereview.com/News")
      soup = BeautifulSoup(page.content, "html.parser")
      li_box = soup.select('div.col-sm-5 > ul > li > h5 > a')
      for link in li_box:
          print(link['href'])
      

      输出:

      /Mock-News-Article/id/42945/Type/eDaily/New-Zealand-Govt-starts-public-consultation-phase-of-review-of-insurance-law
      /Mock-News-Article/id/42946/Type/eDaily/India-M-A-deals-brewing-in-insurance-sector
      /Mock-News-Article/id/42947/Type/eDaily/China-Online-insurance-premiums-soar-31-in-1Q2018
      /Mock-News-Article/id/42948/Type/eDaily/South-Korea-Courts-increasingly-see-65-as-retirement-age
      

      【讨论】:

      • 最好使用响应content 而不是text - 为什么?它不是。查看问题12。解析 HTML 时使用 .text.,下载文件或相关内容时(当您需要二进制数据时)使用 .content。所以,这里最好使用text
      【解决方案3】:

      h5 标签不存在类

      试试这个,

      select_div = soup.findAll('div', {'class': 'col-sm-5'})
      result = []
      for each_div in select_div:
          links = each_div.findAll('a');
          for each_tag in links:
              link = each_tag.attrs['href']       
              result.append(str(link))
      
      print(result)
      

      输出将是 url 列表

      ['/Mock-News-Article/id/42945/Type/eDaily/New-Zealand-Govt-starts-public-consultation-phase-of-review-of-insurance-law', '/Mock-News -Article/id/42946/Type/eDaily/India-M-A-deals-brewing-in-insurance-sector', '/Mock-News-Article/id/42947/Type/eDaily/China-Online-insurance-premiums- soar-31-in-1Q2018', '/Mock-News-Article/id/42948/Type/eDaily/South-Korea-Courts-increasingly-see-65-as-retirement-age']

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-08
        • 2021-05-29
        • 1970-01-01
        • 1970-01-01
        • 2018-05-31
        • 1970-01-01
        • 1970-01-01
        • 2019-12-04
        相关资源
        最近更新 更多