I have a list of web-pages that I'm looping through. My goal is to get text from each of these web-pages IF there is something. I'm looking for certain -tag elements that have same className. First part (looping) is working perfectly but for some reason i'm getting only value [None] while printing this -tags text.
here is what i'm doing:
jotain = []
col = 3
for row, urls in enumerate(links):
page = requests.get(urls)
soup = BeautifulSoup(page.content, 'html.parser')
sposti = soup.find('a', {'class' : '_contactLink_1i8pl_1'})
jotain.append(sposti)
print(jotain.text)
sheet.write(row, col, sposti)
It returns me: "AttributeError: 'list' object has no attribute 'text'"
And this is how the html looks like:
<a class="_contactLink_1i8pl_1" href="mailto:info@talligym.fi">info@talligym.fi</a>
Thanks for help in advance!
答案 0 :(得分:2)
jotain是保存您的属性元素的列表,请尝试
jotain.append(sposti.text)
print(jotain)
编辑:通过打印变量“ sposti”来进行调试,检查是否确实要首先获取数据。
答案 1 :(得分:1)
您要将在soup
中找到的数据追加到列表jotain
,而无需在列表中调用jotain.text
,这就是您阅读在soup
中找到的文本。您需要将文本部分从soup
附加到jotain
;
jotain.append(sposti.text)
只需调用print(jotain)
,它将返回您需要的内容。
编辑:如果您有多个匹配标签,您可能还想使用soup.findall
,否则它只会返回第一个找到的值。
答案 2 :(得分:1)
您已将标签附加到列表中,并尝试从该列表中获取文本。这就是引发错误的原因。试试这个。
jotain.append(sposti.text)
print(jotain)
您也可以尝试列表理解。
jotain.append(sposti)
jotain = [i.text for i in jotain]
print(jotain)
希望这可以使您更清楚!干杯!