【问题标题】:lxml returned me a list but it's emptylxml 给我返回了一个列表,但它是空的
【发布时间】:2019-10-15 16:29:57
【问题描述】:

我试图从这个网站列出所有前 1000 名 instagramer 帐户:'https://hypeauditor.com/top-instagram/'。 从 lxml 返回的列表对于 lxml.html 和 lxml.etree 都是空的。

我尝试删除 tbody、删除 text() 和上部 xpath,但都失败了。 值得注意的是,使用上层 xpath,它确实给我返回了一些东西,但它几乎是 /n。

我首先尝试了 lxml.etree

market_url='https://hypeauditor.com/top-instagram/'
r_market=requests.get(market_url)
s_market=etree.HTML(r_market)`
file_market=s_market.xpath('//*[@id="bloggers-top-table"]/tr[1]/td[3]/a/text()')

然后我也尝试了 lxml.html。

tree=html.fromstring(r_market.content)
result=tree.xpath('//*[@id="bloggers-top-table"]/tr/td/h4/text()')

此外,我尝试了这个 xpath:

s_market.xpath('//*[@id="bloggers-top-table"]/tbody/text()')

它没有给我任何错误。但是经过所有的尝试,它仍然给我一个空列表或一个充满 n/ 的列表。

我在网络抓取方面没有真正的经验,所以我可能只是在某个地方犯了一个愚蠢的错误,但是由于没有数据我无法启动我的机器学习模型,我真的很挣扎,请帮助。

【问题讨论】:

  • 您是在整个表格之后还是在某些列之后?
  • 我只关注instagramer账户的栏目。

标签: python html web-scraping lxml xml.etree


【解决方案1】:

更简单的方法是使用pandas。它可以读取像这样的简单 HTML 表格没有问题。尝试使用以下代码抓取整个表格。

import pandas as pd

df = pd.read_html('https://hypeauditor.com/top-instagram/')

【讨论】:

    【解决方案2】:

    这是一种使用 nth-of-type 仅获取该列的更轻量级的方法。您应该更快地找到它。

    import requests
    from bs4 import BeautifulSoup as bs
    
    r = requests.get('https://hypeauditor.com/top-instagram/')
    soup = bs(r.content, 'lxml')
    accounts = [item.text.strip().split('\n') for item in soup.select('#bloggers-top-table td:nth-of-type(4)')][1:]
    print(accounts)
    

    【讨论】:

      【解决方案3】:

      您一定会想熟悉 BeautifulSoup 包,它允许您在 python 中导航网页的内容。

      使用 BeautifulSoup:

      import requests
      from bs4 import BeautifulSoup
      
      url = 'https://hypeauditor.com/top-instagram/'
      r = requests.get(url)
      html = r.text
      
      soup = BeautifulSoup(html, 'html.parser')
      
      top_bloggers = soup.find('table', id="bloggers-top-table")
      table_body = top_bloggers.find('tbody')
      rows = table_body.find_all('tr')
      
      # For all data:
      # Will retrieve a list of lists, good for inputting to pandas
      
      data=[]
      
      for row in rows:
          cols = row.find_all('td')
          cols = [ele.text.strip() for ele in cols]
          data.append([ele for ele in cols if ele]) # Get rid of empty values
      
      
      # For just handles:
      # Will retrieve a list of handles, only
      
      handles=[]
      
      for row in rows:
          cols = row.find_all('td')
          values = cols[3].text.strip().split('\n')
          handles.append(values[-1])
      

      我用于行的 for 循环来自 answer

      【讨论】:

      • 感谢您详细而热情的回答,我的问题已经解决了。我肯定会研究美丽的汤。
      • 再问一个问题,希望不要问太多。如何废弃表格的所有页面而不是第一页?
      • 您是在询问此特定网页上的其他表格吗?还是其他网页?
      • 喜欢,在 'hypeauditor.com/top-instagram/p=2' 上获取表格,一直到 20。
      • 您可以构建一个 for 循环或 while 循环来循环遍历 url 中的每个 id。例如:urls = ['https://hypeauditor.com/top-instagram/p2=p{i}') for i in range(1,100)]
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-18
      相关资源
      最近更新 更多