Youtube 中的请求 vs Selenium Python [英] Requests vs Selenium Python in Youtube

查看:34
本文介绍了Youtube 中的请求 vs Selenium Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 Selenium 库 在 YouTube 频道页面中查找相关频道的长度时它给了我 12.但是当我使用 请求库 以找到长度它给我 0.

When I use Selenium library to find the length of related channel in YouTube channel Page it gives me 12. But when I use Requests library to find the length it gives me 0.

我想使用请求,如果可能的话请帮助我

I want to use requests please help me if it's possible

我的代码

import requests
from bs4 import BeautifulSoup
import time
r = requests.get("https://www.youtube.com/channel/UCoykjkkJxsz7JukJR7mGrwg/about")
soup = BeautifulSoup(r.content, 'html.parser')
bb = soup.find_all("ytd-mini-channel-renderer",class_="style-scope ytd-vertical-channel-section-renderer")
print(len(bb))

from selenium import webdriver
from bs4 import BeautifulSoup
import time
driver = webdriver.Chrome(chrome_path)
driver.get("https://www.youtube.com/channel/UCoykjkkJxsz7JukJR7mGrwg/about")
soup = BeautifulSoup(driver.page_source, 'html.parser')
bb = soup.find_all("ytd-mini-channel-renderer",class_="style-scope ytd-vertical-channel-section-renderer")
print(len(bb))

推荐答案

每次我遇到这样的问题,都是因为 JS 正在创建我想要的数据.如果是这种情况,您可能无法使用请求,因为它无法处理 JS.

Every time I've run into an issue like this, it was because JS was creating the data I was after. If this is the case, you likely won't be able to use requests as it can't handle the JS.

如果您在浏览器中导航到该 youtube 页面,如果您检查它,您可以看到ytd-mini-channel-renderer"存在,但如果您查看源代码,您会得到 0 个结果.您可以从查看源代码"中看到的代码是请求获得的内容.

If you navigate to that youtube page in a browser, you can see that "ytd-mini-channel-renderer" exists if you inspect it, but if you view source, you get 0 results. The code you can see from "view source" is what requests is getting.

这篇关于Youtube 中的请求 vs Selenium Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆