首页 > 解决方案 > 使用 selenium python 在 youtube 文本字段中输入自定义文本

问题描述

我正在为 youtube 制作一个文本抓取工具,我想在其中输入数据并搜索视频并收集它的数据。我在文本字段中输入数据时遇到问题。谁能建议我一种方法来做到这一点?

from bs4 import BeautifulSoup 

driver = webdriver.Chrome()
soup = BeautifulSoup(driver.page_source, 'lxml') #Use the page as source

page = driver.get('https://freight.rivigo.com/dashboard/home')

import sys

from importlib import reload
reload


elem = driver.find_element_by_tag_name("body")

no_of_pagedowns = 120

while no_of_pagedowns:
    elem.send_keys(Keys.PAGE_DOWN)
    time.sleep(0.5)
    no_of_pagedowns-=1

soup = BeautifulSoup(driver.page_source, 'lxml')

在这段代码之间,我想在输入字段中添加一个自定义文本,让我们说“喜剧”并想要获取相关数据。我被困在如何输入数据上,而且我对此很陌生,所以任何形式的帮助都会有所帮助。

标签: seleniumselenium-webdriverbeautifulsoupselenium-ide

解决方案


该页面未指向 YouTube。查看下面的工作代码示例,了解您可以使用 YouTube API 做什么。

# https://medium.com/greyatom/youtube-data-in-python-6147160c5833
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
#from youtube_data import youtube_search


test = youtube_search("Nine Inch Nails")
test.keys()

test['commentCount'][:5]

df = pd.DataFrame(data=test)
df.head()

df1 = df[['title','viewCount','channelTitle','commentCount','likeCount','dislikeCount','tags','favoriteCount','videoId','channelId','categoryId']]
df1.columns = ['Title','viewCount','channelTitle','commentCount','likeCount','dislikeCount','tags','favoriteCount','videoId','channelId','categoryId']
df1.head()


#import numpy as np
#numeric_dtype = ['viewCount','commentCount','likeCount','dislikeCount','favoriteCount']
#for i in numeric_dtype:
#    df1[i] = df[i].astype(int)

NIN = df1[df1['channelTitle']=='Nine Inch Nails']
NIN.head()


NIN = NIN.sort_values(ascending=False,by='viewCount')
plt.bar(range(NIN.shape[0]),NIN['viewCount'])
plt.xticks(range(NIN.shape[0]),NIN['Title'],rotation=90)
plt.ylabel('viewCount in 100 millions')

plt.show()

推荐阅读