我从 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/