python - 属性错误: 'function' object has no attribute 'read'

标签 python python-3.x youtube

我从 youtube 获取(搜索)数据的程序出现错误,它显示错误 AttributeError: 'function' object has no attribute 'read' i am on python3

import urllib.request
from bs4 import BeautifulSoup
import sys

flag = 0
textToSearch = 'hello world'
query = sys.argv[0].strip("\"").replace(" ","+")
url = "https://www.youtube.com/results?search_query=" + query

response = urllib.request.urlopen
html = response.read()

soup = BeautifulSoup(html,"lxml")
for vid in soup.findAll(attrs={'class':'yt-uix-tile-link'}):
if ('https://www.youtube.com' + vid['href']).startswith("https://www.youtube.com/watch?v="):
    flag = 1
print ('https://www.youtube.com' + vid['href'])
if flag == 0:
    print ("No results found") ```

最佳答案

这里犯了错误:

response = urllib.request.urlopen
html = response.read()

您将 urllib.request.urlopen 放入 response 变量中,而不是调用该函数的结果。

所以而不是

response = urllib.request.urlopen

您应该使用适当的参数调用它:

response = urllib.request.urlopen( .. parameters come here ... )

关于python - 属性错误: 'function' object has no attribute 'read' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55992152/

相关文章:

python - 如何在python中保存单个键的多个值

python - 立即将用户输入输出到控制台

python-3.x - 如何在virtualenv中安装gcc

youtube - 我可以使用以前上传的youtube视频开始youtube实时流吗?

python - 负载和评分之间的因子顺序不同(使用oblimin旋转),这怎么可能?

python - 使用 Spacy 处理语法错误

video - youtube:将视频发送到 channel 使用API

jquery - youtube-video 后面的下拉菜单

python - Redis 对象映射器的原子事务?

python - 数据不会在文档中解析,但会在 Python 中解析为虚拟数据