使用Selenium Python在youtube文本字段中输入自定义文本

时间:2019-06-14 23:07:36

标签: selenium selenium-webdriver beautifulsoup selenium-ide

我正在为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')

在这段代码之间,我想在输入字段中添加自定义文本,让我们说“喜剧”,并希望获取有关该数据的信息。我一直在坚持如何输入数据,对此我还很陌生,因此任何形式的帮助都将对您有所帮助。

1 个答案:

答案 0 :(得分:0)

该页面未指向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()