【问题标题】:Retriving value from href using python selenium使用python selenium从href中检索值
【发布时间】:2021-09-08 16:43:57
【问题描述】:
<a class="yt-simple-endpoint style-scope yt-formatted-string" spellcheck="false" href="/channel/UC8butISFwT-Wl7EV0hUK0BQ" dir="auto">freeCodeCamp.org</a>

我是 selenium 的新手,正在尝试制作一个 youtube 排名检查机器人! 我正在尝试从此处获取 href 值,以便将其与频道名称进行比较并打印出正确的排名编号,但我得到的输出不正确。 我得到的输出是 2,5,而我应该得到 6,7。

谁能告诉我哪里/我做错了什么?可以做些什么来解决这个问题?提前谢谢

下面附上截图查看排名

from selenium import webdriver
import time
channel_name = 'freeCodeCamp.org' #channel name
driver = webdriver.Chrome(r"C:\\Users\\user\\PycharmProjects\\YoutubeRankCheckBot\\Drivers\\chromedriver.exe")
driver.get("http://youtube.com")
driver.maximize_window()

search_bar = driver.find_element_by_id("search")
search_bar.send_keys("React JS") #Inserting text input in a automation way
search_button = driver.find_element_by_id("search-icon-legacy")
search_button.click()

time.sleep(5)


video_list = driver.find_elements_by_xpath('//a[contains(@href,"/channel/UC8butISFwT-Wl7EV0hUK0BQ")]')
print(video_list)

for index, channel in enumerate(video_list):
    if channel.text  == channel_name:
        print(index)

【问题讨论】:

    标签: python selenium bots ranking browser-automation


    【解决方案1】:
     video_list = driver.find_elements_by_xpath('//a[@class="style-scope ytd-video-renderer"]')
    video_url = [video_list.get_attribute('href').replace('https://www.youtube.com', '') for video_list in video_list]
    print(video_url)
    
    for index, channel in enumerate(video_url):
        if channel == channel_id:
            print(index)
    

    这里的channel_id 只不过是/channel/UC8butISFwT-Wl7EV0hUK0BQ 的频道名称freeCodeCamp.org。

    1. 第一行给出a标签的所有元素
    2. 从步骤 1 的列表中,找到 href 元素。它将为您提供完整的 URL,例如https://www.youtube.com/channel/UC8butISFwT-Wl7EV0hUK0BQ。所以替换https://www.youtube.com 并提取到video_url
    3. 使用video_url 枚举并打印index

    【讨论】:

    • ['/user/TechGuyWeb', '/user/programmingwithmosh', '/user/programmingwithmosh', '/channel/UC8butISFwT-Wl7EV0hUK0BQ',......] video_url 列表是在 index = 1 和 2 处显示两次 mosh 编程,但 mosh 在 index = 1 的排名中仅出现一次。然后在索引号 3 和 4 中出现了两次所需的 url,但在排名中,它排在第 7 和第 8 位(索引 = 6 & 7)
    • user/programmingwithmosh 有两个条目 - 1. React JS - React 初学者教程和 2. 什么是 React (React JS) & 为什么它如此受欢迎?
    • 注意:来自 youtube 的建议可能因人而异。因为我收到了两次user/programmingwithmosh
    • 是的,我知道建议因用户而异。但是,这是你通过执行代码根据建议模式得到的正确结果,我不应该在执行代码时根据我的建议模式得到输出吗? :(
    • 等等,它正在工作,但它没有计算播放列表。没关系,我猜。非常感谢你。我无法用言语表达我的感激之情。在过去的 2.5 天里,我一直在处理它。谢谢@Vijay Dodamani 兄弟
    【解决方案2】:

    您使用了错误的定位器。
    试试这个:

    video_list = driver.find_elements_by_xpath("//div[@id='channel-info']//a[@class='yt-simple-endpoint style-scope yt-formatted-string']")
    

    UPD
    经过你的解释,我明白了一点。
    您可以通过以下方式找到这些特定元素:

    //div[@id='channel-info']//a[@class='yt-simple-endpoint style-scope yt-formatted-string' and (contains(@href,"/channel/UC8butISFwT-Wl7EV0hUK0BQ"))]
    

    这将为您提供 2 个元素,因为该搜索结果中有来自该频道的 2 个视频

    【讨论】:

    • 我以前试过,现在,我用你的解决方案再次运行代码,但得到了 3,5!这次排名发生了一些变化,所以我应该得到 6 和 8。:(
    • 我真的不明白你想做什么?问题标题不是我在问题正文中看到的......你能澄清一下吗?
    • 好的,我现在知道我做了什么来迷惑你!我很抱歉。我要做的是列出所有href值并将其与我的特定href值匹配(“/channel/UC8butISFwT-Wl7EV0hUK0BQ”)匹配的那些,匹配的索引将是我的答案。但到目前为止,没有运气。我正在处理它几乎 24 小时,并在我自己没有解决任何解决方案之后将其发布在这里。任何帮助都会非常感激。
    • 嗯,现在我明白了一点。请看看这是否是您要找的?
    • 我将你的 sn-p 复制并粘贴到 driver.find_elements_by_xpath("") 中,但有一点语法错误,所以我修改了一下,但仍然没有运气。现在它显示 0 和 1! :( 修改:video_list = driver.find_elements_by_xpath("//div[@id='channel-info']//a[@class='yt-simple-endpoint style-scope yt-formatted-string' and (contains( @href,'/channel/UC8butISFwT-Wl7EV0hUK0BQ'))]")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 2023-03-29
    相关资源
    最近更新 更多