Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.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
Python 使用bs4搜索Youtube并获得30多个结果_Python_Parsing_Beautifulsoup_Youtube_Request - Fatal编程技术网

Python 使用bs4搜索Youtube并获得30多个结果

Python 使用bs4搜索Youtube并获得30多个结果,python,parsing,beautifulsoup,youtube,request,Python,Parsing,Beautifulsoup,Youtube,Request,我使用此代码进行yt搜索,但最大结果是30个,如何获得更多(例如100个)结果? 有时在response仅{“reload”:“now”}中,它是什么意思 import requests from bs4 import BeautifulSoup import urllib.parse import json def search(search_request): encoded_search = urllib.parse.quote(search_request) BASE

我使用此代码进行yt搜索,但最大结果是30个,如何获得更多(例如100个)结果? 有时在
response
{“reload”:“now”}
中,它是什么意思

import requests
from bs4 import BeautifulSoup
import urllib.parse
import json


def search(search_request):
    encoded_search = urllib.parse.quote(search_request)
    BASE_URL = "https://youtube.com"
    url = f"{BASE_URL}/results?search_query={encoded_search}&pbj=1"
    response = BeautifulSoup(requests.get(url).text, "html.parser")
    results = parse_html(response)

    print(len(results))
    return results

def parse_html(soup):
    results = []
    for video in soup.select(".yt-uix-tile-link"):
        if video["href"].startswith("/watch?v="):
            video_info = {
                "title": video["title"],
                "link": video["href"],
                "id": video["href"][video["href"].index("=")+1:]
            }
            results.append(video_info)
    return results

search('backing track')

您可能想看看pytube,它可以帮助您完成与youtube相关的python任务,而不是通过bs@Tom查看了pytube的文档,但没有找到搜索功能这里的“搜索”功能可以工作吗?client.video_search(q=None,**查询)返回与查询参数匹配的视频流。您可以非常简单地执行基本搜索:vids=c.video_search(“搜索词”)@Tom噢,对不起,我看了一下pytube 3 lib,认为它是pytube的第三个python@TheHalf-血王子是对的,硒是一个更好的选择。您还可以在浏览器处理过程中观察它,这样您就可以看到它出了什么问题(可视化调试哈哈)。当一切正常时,你也可以使用无头浏览器,它们的性能会更快。通过多线程,您还可以同时运行多个浏览器