無法決議來自https://www.ted.com/talks/alexis_nikole_nelson_a_flavorful_field_guide_to_foraging/transcript的視頻轉錄
請求不會看到文本實際所在的跨度類。可能是什么問題呢?
import requests
url = 'https://www.ted.com/talks/alexis_nikole_nelson_a_flavorful_field_guide_to_foraging/transcript'
page = requests.get(url)
print(page.content)
有什么辦法可以拿到成績單嗎?謝謝你。 我需要達到這個 沒有找到屬性
uj5u.com熱心網友回復:
這是因為資料不是通過您使用的鏈接加載的,而是通過呼叫他們的 GraphQL 實體來加載的。
使用 curl,您可以像這樣獲取資料:
curl 'https://www.ted.com/graphql?operationName=Transcript&variables={"id":"alexis_nikole_nelson_a_flavorful_field_guide_to_foraging","language":"en"}&extensions={"persistedQuery":{"version":1,"sha256Hash":"18f8e983b84c734317ae9388c83a13bc98702921b141c2124b3ce4aeb6c48ef6"}}' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:99.0) Gecko/20100101 Firefox/99.0' -H 'Accept: */*' -H 'Accept-Language: en-US,en;q=0.5' -H 'Accept-Encoding: gzip, deflate, br' -H 'Referer: https://www.ted.com/talks/alexis_nikole_nelson_a_flavorful_field_guide_to_foraging/transcript' -H 'content-type: application/json' -H 'client-id: Zenith production' -H 'x-operation-name: Transcript' --output - | gzip -d
請注意,URL 是 urlencoded。您可以匯入from urllib.parse import quote以使用該quote()方法在 python 中對字串進行 urlencode。
所以只需將上面的 curl 命令翻譯成 python。沒有魔法,只需設定正確的標題。如果你很懶,你也可以使用這個在線轉換器,將 curl 命令轉換為 python 代碼。
這會產生:
import requests
from requests.structures import CaseInsensitiveDict
url = "https://www.ted.com/graphql?operationName=Transcript&variables={"id":"alexis_nikole_nelson_a_flavorful_field_guide_to_foraging","language":"en"}&extensions={"persistedQuery":{"version":1,"sha256Hash":"18f8e983b84c734317ae9388c83a13bc98702921b141c2124b3ce4aeb6c48ef6"}}"
headers = CaseInsensitiveDict()
headers["User-Agent"] = "Mozilla/5.0 (X11; Linux x86_64; rv:99.0) Gecko/20100101 Firefox/99.0"
headers["Accept"] = "*/*"
headers["Accept-Language"] = "en-US,en;q=0.5"
headers["Accept-Encoding"] = "gzip, deflate, br"
headers["Referer"] = "https://www.ted.com/talks/alexis_nikole_nelson_a_flavorful_field_guide_to_foraging/transcript"
headers["content-type"] = "application/json"
headers["client-id"] = "Zenith production"
headers["x-operation-name"] = "Transcript"
resp = requests.get(url, headers=headers)
print(resp.content)
輸出:
b'{"data":{"translation":{"id":"209255","language" ...
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/484529.html
