【问题标题】:How to find the amount of upvotes on a reddit post如何在 reddit 帖子上找到赞成票的数量
【发布时间】:2020-09-18 07:47:52
【问题描述】:
    import results as results
    import soup as soup
    from bs4 import BeautifulSoup
    import requests
    import os, os.path, csv

    from sqlalchemy.sql.operators import div

    page = requests.get(URL)

    soup = BeautifulSoup(page.content, 'html.parser')

    UpvoteCount = results.find('section',div._1rZYMD_4xY3gRcSS3p8ODO)

    print(results.prettify())

    print(soup.style)

我想在 reddit 帖子上找到支持的数量,我找到了执行此操作的代码部分 (这是一个 html 网站,但我的代码是在 python 中的):

    <div class="_1rZYMD_4xY3gRcSS3p8ODO" style="color: rgb(215, 218, 220);">1</div>

我想要找到的重要的是 div 部分 >1

样式是我希望我的代码找到的样式。

【问题讨论】:

  • 如果我没听错的话,你想找出所有有style="color: rgb(215, 218, 220);"&lt;div&gt; 吗?
  • 没有后面的部分,它说 1,这是赞成票数。
  • 我发布了答案。

标签: python html python-3.x web-scraping beautifulsoup


【解决方案1】:

要从样式包含rgb(215, 218, 220) 的元素中获取文本,可以使用以下示例:

from bs4 import BeautifulSoup


page_html = '''
    <div class="_1rZYMD_4xY3gRcSS3p8ODO" style="color: rgb(215, 218, 220);">1</div>
'''

soup = BeautifulSoup(page_html, 'html.parser')

upvote_count = soup.select_one('div[style*="rgb(215, 218, 220)"]').text
print(upvote_count)

打印:

1

【讨论】:

    【解决方案2】:

    可以使用以下方法按类查找 div 元素:

    UpvoteCount = results.find('div', {"class": "_1rZYMD_4xY3gRcSS3p8ODO"})
    

    要查找与条件匹配的所有元素,请使用方法 findAll()。返回的是找到的元素列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-06
      • 2013-03-25
      • 2014-10-11
      • 1970-01-01
      • 1970-01-01
      • 2017-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多