我试图将一串单词添加到列表中,但当我尝试为该列表编制索引时,它会返回单个字母
例如:
url = 'https://almostginger.com/famous-movie-locations/'
titles = []
r = requests.get(url)
soup = BeautifulSoup(r.content, 'lxml')
titles = soup.select('h3')
for t in titles:
tt = t.text.strip()
for s in range(len(tt)):
print(s)
显示仅为单个字母编制索引,而如果我尝试创建列表,则会出现以下错误:
titles.append(tt)
AttributeError: 'str' object has no attribute 'text'
预期成果:
'Café des Deux Moulins as seen in Amélie (2001)',
'Royal Palace of Caserta as seen in Angels and Demons (2009)'
您会因为重复的变量名而出现错误。将其中一个
titles
更改为其他内容输出:
这是一个很好的列表理解用例:
相关问题 更多 >
编程相关推荐