【发布时间】:2017-09-03 09:35:14
【问题描述】:
import requests
from bs4 import BeautifulSoup
youtube = "https://www.youtube.com/results?search_query="
def get_address(keyword):
query = youtube + keyword
source_code = requests.get(query)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, "html.parser")
for link in soup.findAll('a', {'id': 'video-title'}):
href = link.get('href')
print(href)
break
get_address("scishow")
程序运行成功,但没有显示视频的地址,输出中什么也没有显示。
【问题讨论】:
-
您的代码很好,它没有在输出中显示任何内容的唯一原因是您要查找的
a标记不存在。<a id="video-title" ...>稍后会使用 JavaScript 添加到页面中,当您检索初始 HTML 代码时,它当然还没有执行。 -
很可能是因为页面使用了
JS。在这种情况下请求将无用,请改用selenium。
标签: python beautifulsoup web-crawler