import requests
from lxml import html
response = requests.get('https://www.youtube.com/results?search_query=gorrilaz&sp=CAE%253D')
parser_tree = html.fromstring(response.content)
content = parser_tree.xpath('//*[contains(@class, "yt-lockup-thumbnail")]/a[@href]')
print(content)
print(type(content[0]))
我正在尝试获取页面上的所有链接。答案为何而来
"元素 a 位于 0x7ff2295f80e8">
?
在变量中
content你有类对象lxml.html.HtmlElement为了拉出链接,我们可以get('href')在每个这样的元素上调用一个方法: