【问题标题】:scrape question answers, date and upvotes from quora从 quora 中抓取问题答案、日期和赞成票
【发布时间】:2020-01-24 16:26:37
【问题描述】:

我正在尝试使用beautifulsoup 从这个answer 中提取答案、日期和投票数字 - 但是我无法选择class="pagedlist_item"。我想从包括每个答案的内容的这门课开始的原因是,例如,有些帖子没有赞成票,所以我最终会得到不同长度的元素列表,以防缺少某些东西以及混合相同变量的顺序。

items_soup = BeautifulSoup(html, "html")
items_soup.find_all("div", {"class" : "pagedlist_item"})

当我运行此代码时,它返回一个空列表 - 所以不确定出了什么问题? 然后我想从中提取答案的文本、日期和赞成数字(即使没有 - 所以基本上用 0 替换空的)。

是否可以拆分并获取我列出的每个元素?答案文本、答案日期和答案的投票数字 - 目的是然后创建一个数据框。

请记住:帖子有 49 个答案,但如果您不向下滚动,则不会显示所有答案,我想抓取所有 49 个答案。

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:

    我可以通过以下代码获得您要查找的内容:

    import requests
    from bs4 import BeautifulSoup
    
    url = 'https://www.quora.com/What-is-the-brutal-truth-about-data-scientists'
    r = requests.get(url)
    soup = BeautifulSoup(r.text, 'lxml')
    
    question = soup.find('span', {'class': 'ui_qtext_rendered_qtext'})
    answers = [ s.text for s in soup.find_all("div", {"class" : "pagedlist_item"}) if s.text ]
    

    产生question == 'What is the brutal truth about data scientists?' 和一个包含 28 个答案的列表。

    【讨论】:

    • 谢谢 - 这将返回文本,但它还包括每个帖子的用户名、日期和其他元素作为字符串。是否可以拆分并获取我列出的每个元素?答案文本,答案日期和答案的投票数字 - 目的是然后创建一个数据框。另外,帖子有 49 个答案而不是 28 个?
    • 您能否更新您的问题以包含所有这些信息?
    【解决方案2】:

    运行以下命令时没有空列表:

    import requests
    from bs4 import BeautifulSoup
    
    html ='https://www.quora.com/What-is-the-brutal-truth-about-data-scientists'
    r = requests.get(url).text
    soup = BeautifulSoup(r, 'html')
    soup.find_all("div", {"class" : "pagedlist_item"})
    

    请检查一下!不确定您是否包含请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      相关资源
      最近更新 更多