简体   繁体   English

这段代码有问题吗? (Python爬行)

[英]Is there any problem in this code? (Python crawling)

I don't know why there is an error:我不知道为什么会出现错误:

TypeError: list indices must be integers or slices, not str

This URL is a Korean question website.这个网址是一个韩语问答网站。

import requests

from bs4 import BeautifulSoup

url = "http://www.algotank.com/bbs/board.php?bo_table=algotank&wr_id=16"

source = requests.get(url).text
soup = BeautifulSoup(source, 'html.parser')
AnsInput = soup.find_all("input",{"name":"AnsInput"})['answer']

print(AnsInput)

BeautifulSoup 的find_all将返回一个列表,而不是一个字典,所以如果你想要一个特定的元素,需要使用一个整数来索引,例如

AnsInput = soup.find_all("input",{"name":"AnsInput"})[0]['answer']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM