第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Python Youtube Web Scraper 無法正常工作

Python Youtube Web Scraper 無法正常工作

慕標(biāo)琳琳 2023-03-08 15:34:27
所以我構(gòu)建了這個(gè)小腳本,可以返回 YouTube 上任何搜索到的視頻的 URL。但是在再次打開它之后發(fā)現(xiàn)使用 youtube 進(jìn)行的網(wǎng)絡(luò)抓取無法正常工作。在打印時(shí),soup它返回的內(nèi)容與在 Youtube 上使用 inspect element 看到的內(nèi)容完全不同。有人能幫我解決這個(gè)問題嗎……這是我的代碼:import requestsfrom lxml import htmlimport webbrowserfrom bs4 import BeautifulSoupimport timeimport tkinterfrom pytube import YouTubeheaders= {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36"}def video_finder():    word = input("Enter video title: ")    if ' ' in word:        new = word.replace(' ', '+')        print(new)    else:        pass    vid = requests.get('https://www.youtube.com/results?search_query={}'.format(new))    soup = BeautifulSoup(vid.text, features='lxml')    all_vids = soup.find_all('div', id_='contents')    print(all_vids)    video1st = all_vids[0]    a_Tag = video1st.find('a', class_="yt-uix-tile-link yt-ui-ellipsis yt-ui-ellipsis-2 yt-uix-sessionlink spf-link", href=True)    Video_name = a_Tag.text    Video_id = a_Tag['href']    video_link = 'https://www.youtube.com' + Video_id    print(Video_name)    print(video_link)它不是最好的但是你...謝謝
查看完整描述

1 回答

?
慕妹3242003

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超6個(gè)贊

要從 Youtube 頁面獲得正確的結(jié)果,請(qǐng)將User-AgentHTTP 標(biāo)頭設(shè)置為 Googlebot,并 html.parser在 BeautifulSoup 中使用。


例如:


import requests

from bs4 import BeautifulSoup



headers= {"User-Agent": "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"}

def video_finder():

    word = input("Enter video title: ")


    params = {

        'search_query': word

    }


    vid = requests.get('https://www.youtube.com/results', params=params, headers=headers)

    soup = BeautifulSoup(vid.content, features='html.parser')

    a_Tag = soup.find('a', class_="yt-uix-tile-link yt-ui-ellipsis yt-ui-ellipsis-2 yt-uix-sessionlink spf-link", href=lambda h: h.startswith('/watch?'))

    Video_name = a_Tag.text

    Video_id = a_Tag['href']

    video_link = 'https://www.youtube.com' + Video_id

    print(Video_name)

    print(video_link)


video_finder()

印刷:


Enter video title: sailor moon

Sailor Moon Opening (English) *HD*

https://www.youtube.com/watch?v=5txHGxJRwtQ


查看完整回答
反對(duì) 回復(fù) 2023-03-08
  • 1 回答
  • 0 關(guān)注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)