我正试图通过使用lxml.html进行解析来获取YouTube上的电影预告片:
from lxml import html
import lxml.html
from lxml.etree import XPath
def get_youtube_trailer(selected_movie):
# Create the url for the YouTube query in order to find the movie trailer
title = selected_movie
t = {'search_query' : title + ' movie trailer'}
query_youtube = urllib.urlencode(t)
search_url_youtube = 'https://www.youtube.com/results?' + query_youtube
# Define the XPath for the YouTube movie trailer link
movie_trailer_xpath = XPath('//ol[@class="item-section"]/li[1]/div/div/div[2]/h3/a/@href')
# Parse the YouTube html code
html = lxml.html.parse(search_url_youtube)
# Add the movie trailer to our results
results['movie_trailer'] = …Run Code Online (Sandbox Code Playgroud)