【问题标题】:How do I look for the right class and id for parsing a page?如何寻找正确的类和 id 来解析页面?
【发布时间】:2020-05-19 07:14:05
【问题描述】:

这是我到目前为止的代码,我正在尝试获取订阅者数量。 这是我得到的错误:

AttributeError: 'NoneType' object has no attribute 'find_all'

希望有人能帮忙!

    from bs4 import BeautifulSoup
    import requests as r

    url = r.get("https://www.youtube.com/channel/UC57EgpLB1Q0tXc5tWDhttoQ)
    soup_content = BeautifulSoup(url.content, 'html.parser')
    id_ = soup_content.find(id="meta")
    class_ = id_.find_all(class_="style-scope ytd-c4-tabbed-header-renderer")
    hopefully_it_works = class_[0]
    print(hopefully_it_works.prettify())

【问题讨论】:

标签: python


【解决方案1】:

BS 和网络抓取不是获取 youtube 数据的正确方法。 请考虑使用官方的 youtube api,它提供了您需要的这些数据以及更多:) https://developers.google.com/youtube/v3/docs/

你可以在这里https://developers.google.com/youtube/v3/docs/channels/list#try-it尝试你的请求

在您的情况下,您需要填写: 部分:统计 编号:UC57EgpLB1Q0tXc5tWDhttoQ (https://www.googleapis.com/youtube/v3/channelspart=statistics&id=channel_id&key=your_key)

回应是

{
  "kind": "youtube#channelListResponse",
  "etag": "eQRUDH-2j1eYIpexSuSOsz12tc8",
  "pageInfo": {
    "totalResults": 1,
    "resultsPerPage": 1
  },
  "items": [
    {
      "kind": "youtube#channel",
      "etag": "D-yZ896UMFRcDDSrfATBaiygDkc",
      "id": "UC57EgpLB1Q0tXc5tWDhttoQ",
      "statistics": {
        "viewCount": "9",
        "commentCount": "0",
        "subscriberCount": "3",
        "hiddenSubscriberCount": false,
        "videoCount": "1"
      }
    }
  ]
}

编辑: 如果你真的想使用 BS,有你的解决方案:

from bs4 import BeautifulSoup as bs
import requests as r

content = r.get("https://www.youtube.com/channel/UC57EgpLB1Q0tXc5tWDhttoQ")
soup = bs(content.content, "html.parser")
channel_subscribers = soup.find("span", attrs={"class": "channel-header-subscription-button-container yt-uix-button-subscription-container with-preferences"}).find("span", attrs={"class": "yt-subscription-button-subscriber-count-branded-horizontal subscribed yt-uix-tooltip"}).text
print(channel_subscribers)

【讨论】:

  • idk 你使用的语言是什么,我也只是在练习网页抓取,我想要一个实际的用途,所以我认为“yt 频道的订阅者”是一个很好的选择。我的代码是不是以严肃的方式工作(比如它的所有内容都是错误的?)还是不那么严肃的方式(比如 1 或 2 行/调整)?
  • 查看更新后的答案。我希望这是你想要的:)
  • 谢谢 :) 是的,我可能不会再尝试网络抓取了,但我想我可能想尝试一下,并将它添加到我可以在对话中提出的主题列表。
  • 网页抓取是有趣且有用的东西,但总是尝试使用 api 来处理这些东西。如果没有提供 api,网络报废是要走的路:) 如果我回答了你的问题,请随时将其标记为答案 :) 谢谢!
猜你喜欢
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 2010-11-04
  • 1970-01-01
  • 2011-06-07
  • 1970-01-01
  • 2020-11-19
相关资源
最近更新 更多