【问题标题】:requests.get() not returning proper text order when reading youtube HTML阅读 youtube HTML 时,requests.get() 没有返回正确的文本顺序
【发布时间】:2019-11-19 02:12:43
【问题描述】:

我正在使用 requests.get() 来解析 youtube HTML 文本。当我在传递链接后打印输出时,一个视频出现故障。我正在尝试使用 BeautifulSoup 按顺序播放视频,并且出现故障的视频在所有其他视频之前显示。任何建议或可能的修复都会有所帮助。

    true = requests.get(link +searched)
    page = true.text
    #print(page)
    soup = BeautifulSoup(page, 'html.parser')
    #print(soup)
    search_results = soup.findAll('a', attrs={'class': 'yt-uix-tile-link'})
    #print(search_results)

更多参考代码:

import pafy
import vlc
import requests
from bs4 import BeautifulSoup
import time
link="https://www.youtube.com/results?search_query="
youtube = "https://www.youtube.com"
word = "jid playlist"


def findlnks(searched):
    if '&list' not in searched:
        true = requests.get(link +searched)
        page = true.text
        #print(page)
        soup = BeautifulSoup(page, 'html.parser')
        #print(soup)
        search_results = soup.findAll('a', attrs={'class': 'yt-uix-tile-link'})
        #print(search_results)
    else:
        true = requests.get(searched)
        page = true.text
        soup = BeautifulSoup(page, 'html.parser')
        #print(soup)
        search_results = soup.findAll('a', class_ ="spf-link playlist-video clearfix yt-uix-sessionlink spf-link")
        #print(search_results)
    return search_results

if 'mix' in word or 'playlist' in word:
    total_results = findlnks(word)
    i =0
    playlist_size=0
    #while i< 10:
        #print(total_results[i]['title'])
        #i+=1
    while 'list' not in (total_results[i])['href']:
        print(total_results[i]['href'])
        i = i + 1




    playlist_results=findlnks(youtube + ((total_results[i])['href']))

    while playlist_size<40:
        #print(youtube + (playlist_results[playlist_size])['href'])
        playlist_size = playlist_size +1

    while (playlist_results[playlist_size])['href'] !='\0':
        url = youtube + (playlist_results[playlist_size])['href']
        #print(url)
        video = pafy.new(url)
        best = video.getbest()
        playurl = best.url
        Instance = vlc.Instance()
        player = Instance.media_player_new()
        media = Instance.media_new(playurl)

        media.get_mrl()
        player.set_media(media)
        player.play()
        playing = set([1, 2, 3, 4])
        time.sleep(1)
        duration = player.get_length() / 1000
        mm, ss = divmod(duration, 60)

        while True:
            state = player.get_state()
            if state not in playing:
                break
            continue
        playlist_size = playlist_size +1

【问题讨论】:

  • 你能分享你的link+searched吗?
  • 对于您的搜索结果,您到底在寻找什么?
  • @HjSin 我编辑了我的帖子,所以它包含了我到目前为止的大部分内容。这应该提供更好的上下文。这部分是在初始搜索结果中搜索播放列表,然后再次调用 findlks 来获取播放列表中视频的 url。它有效,它们只是不符合 youtube 提供的顺序。你会注意到我还有一个块,它可以播放从 VLC 媒体播放器的播放列表中检索到的视频。一旦另一个视频结束,我在播放视频时遇到了一些问题。

标签: python python-requests


【解决方案1】:

Youtube 网站使用 ajax 技术。 requests 库仅适用于非 ajax 站点。考虑使用无头浏览器从 youtube 获取 html,然后提取数据。 您可以在无头模式或 phantomjs 浏览器中使用 firefox

我找到了这个教程,它可能对你有所帮助。 https://towardsdatascience.com/web-scraping-using-selenium-and-beautifulsoup-99195cd70a58

# Firefox session
driver = webdriver.Firefox()
driver.get(videos_url)
# Try to increase the time if the page need more time to be fully loaded
driver.implicitly_wait(100)

【讨论】:

  • 我该怎么做。我刚开始接触 Python,从来没有做过 Web 开发,所以这些主题对我来说很新鲜。
  • 我找到了关于这个的教程。希望对你有帮助^^
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-27
  • 1970-01-01
相关资源
最近更新 更多