Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Selenium从youtube搜索创建包含N个元素的Python字典_Python_Python 3.x_Selenium_Selenium Webdriver - Fatal编程技术网

使用Selenium从youtube搜索创建包含N个元素的Python字典

使用Selenium从youtube搜索创建包含N个元素的Python字典,python,python-3.x,selenium,selenium-webdriver,Python,Python 3.x,Selenium,Selenium Webdriver,因此,我试图使用Selenium创建一个包含youtube视频N个元素的字典,这些元素的视图来自youtube搜索。 例如:{'videourl01':521,'videourl02':782} 是的,关键是视频的url,索引是浏览量,总共有N个视频 登陆搜索页面并进行搜索后,接下来应该采取什么步骤来实现这一点 非常感谢您的任何帮助:> 到目前为止,已成功获取所有视频标签: def GetTopVideosfromSearch(self,query,N): query = que

因此,我试图使用Selenium创建一个包含youtube视频N个元素的字典,这些元素的视图来自youtube搜索。 例如:{'videourl01':521,'videourl02':782} 是的,关键是视频的url,索引是浏览量,总共有N个视频

登陆搜索页面并进行搜索后,接下来应该采取什么步骤来实现这一点

非常感谢您的任何帮助:>

到目前为止,已成功获取所有视频标签:

    def GetTopVideosfromSearch(self,query,N):
    query = query.replace(' ', '+')
    self.browser.get('https://www.youtube.com/results?search_query='+query)
    vids=self.browser.find_elements_by_id('video-title')
    for vid in vids[0:N]:
        print((vid.get_attribute("aria-label")))

我在过去使用过Selenium,但用于解析其他网站

首先,您需要生成内容,因为YouTube很可能正在使用ajax

这可以通过以下方式实现:

Keys.PAGE_DOWN
生成内容后,必须在生成的html中搜索要查找的元素

就我而言,我在寻找价格:

browser.find_elements_by_class_name("product-info-price")
一旦有了它,您可以像循环一样迭代,并将结果添加到字典中:

以下是完整的代码片段:

# imports
import pandas as pd
import requests
import time
import selenium
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

link = "https://es.wallapop.com/search?catIds=12461&dist=400&publishDate=any"
browser = webdriver.Chrome()
browser.get(link)
time.sleep(1)
body = browser.find_element_by_tag_name("body")
element = browser.find_element_by_class_name('Button')
browser.execute_script("arguments[0].click();", element)

# generate content, scrolling down the webpage
for _ in range(10):
    body.send_keys(Keys.PAGE_DOWN)
    time.sleep(0.1)

# iterate over the elements and append to the list
list_of_prices = []
for price in browser.find_elements_by_class_name("product-info-price"):
    list_of_prices.append(price.text)

找到了一个适合我的解决方案

    def GetTopVideosfromSearch(self,query,N):
    query = query.replace(' ', '+')
    self.browser.get('https://www.youtube.com/results?search_query='+query)
    for _ in range(N-4):
        self.browser.find_element_by_tag_name("body").send_keys(Keys.PAGE_DOWN)
        time.sleep(0.1)
    vids=self.browser.find_elements_by_id('video-title')
    vidsDict={}
    for vid in vids[0:N]:
        tmp = vid.get_attribute("aria-label")
        tmp=tmp[::-1]
        s=0
        views=''
        for t in tmp:
            if t==' ':
                s+=1
            if s==1 and t!=' ' and t!=',':
                views+=t
        views=int(views[::-1])   
        vidsDict[vid.get_attribute("href")] = views
    return vidsDict

到目前为止你试过什么?给我们看一些代码!