大家好,我目前正试图从URL中获取一些数据,然后试图预测该文章应该属于哪一类。 到目前为止,我已经这样做了,但它有一个错误:
info = pd.read_csv('labeled_urls.tsv',sep='\t',header=None)
html, category = [], []
for i in info.index:
response = requests.get(info.iloc[i,0])
soup = BeautifulSoup(response.text, 'html.parser')
html.append([re.sub(r'<.*?>','',
str(soup.findAll(['p','h1','\href="/avtorji/'])))])
category.append(info.iloc[0,i])
data = pd.DataFrame()
data['html'] = html
data['category'] = category
错误是:
IndexError: single positional indexer is out-of-bounds.
有人能帮帮我吗?在
错误很可能是由于将索引传递给
iloc
:loc
需要索引值和列名,而iloc
需要行和列的数字位置。此外,您已经将category
的行和列位置与category.append(info.iloc[0,i])
互换。所以你至少应该:但是当您尝试迭代dataframe的第一列时,上面的代码不是python代码。最好直接使用该列:
^{pr2}$您可以避免iloc调用,而使用},因为您是在索引上操作的,但是在循环中使用}通常没有那么有效。您可以尝试以下代码(插入等待时间):
iterrows
,我认为您必须使用loc
而不是{iloc
和{如果您真的只需要循环中的url,请替换:
^{pr2}$通过类似于:
相关问题 更多 >
编程相关推荐