【发布时间】:2018-10-15 17:08:28
【问题描述】:
我正在尝试使用 python 读取文件并将每一行作为函数的参数。我有一个 AttributeError: 'NoneType' object has no attribute 'text' 错误,我不明白如何解决它。这是我的代码
from requests import get
from bs4 import BeautifulSoup
file = open("applications.txt","r")
appArray = file.readlines()
def app_metadata(app_link):
url = 'https://play.google.com/store/apps/details?id=' + app_link
response = get(url)
html_soup = BeautifulSoup(response.text, 'html.parser')
print(html_soup.find(class_="AHFaub").text)
#print (appArray[0])
#print(type(appArray[0]))
#print(type("com.codebrewgames.pocketcitygame"))
app_metadata(appArray[2])
【问题讨论】:
-
这里唯一合理的解释是
response是None或html_soup.find(class_="AHFaub")正在返回None。 -
请始终在您的问题中包含完整的错误回溯。
-
请编辑您的问题并提供applications.txt文件
-
@g.d.d.c 指出了合理的解释。其中
html-soup.find(class_="AHFaub")几乎肯定是原因并返回None。 -
另外说明,资源文件
applications.txt永远不会关闭。确保执行 file.close() 来释放资源。更好的是使用with子句来确保文件已关闭。例如with open('applications.txt', 'r') as fin: appArray=fin.readlines()
标签: python