python-3.x - TypeError: list indices must be integers or slices, not str 为什么列表索引不是数字
问题描述
import requests
from bs4 import BeautifulSoup
import urllib.request
import re
with open('crawlingweb.csv')as f:
content=f.readlines()
content=[x.strip() for x in content]
for i in content:
content[i].replace('[', '').replace(']', '')
req = requests.get(content[i])
html = req.text
data = re.sub('[^0-9a-zA-Z\\s\\.\\,]', '', string=html).lower()
data = re.sub('<[^>]*>', '', string=html)
data = re.sub('[^ ㄱ-ㅣ가-힣]+', '', string=html)
print(data)
content[i].replace('[', '').replace(']', '') 这一行是错误的我想要的是重复这段代码并爬取内容中的整个地址一次并打印出来。
import requests
from bs4 import BeautifulSoup
import urllib.request
import re
with open('crawlingweb.csv')as f:
content=f.readlines()
content=[x.strip() for x in content]
content[183].replace('[','').replace(']','')
req = requests.get(content[183])
html = req.text
data = re.sub('[^0-9a-zA-Z\\s\\.\\,]', '', string=html).lower()
data = re.sub('<[^>]*>','',string=html)
data = re.sub('[^ ㄱ-ㅣ가-힣]+','',string=html)
print(data)
此代码有效
谢谢你的阅读
解决方案
因为
with open('crawlingweb.csv')as f: # this is a file content=f.readlines() # this is a list of strings content=[x.strip() for x in content] # this is still a list of strings for i in content: # i is a string from your list
你想要的是:
for index,line in enumerate(content):
# to replace the modified contentitem in content:
content[index] = line.replace('[', '').replace(']', '')
req = requests.get(content[index])
# or - to simply fix 'line' before doing your request:
line = line.replace('[', '').replace(']', '')
req = requests.get(line)
或者像这样进一步做:
with open('crawlingweb.csv')as f:
content=f.readlines()
content=[x.strip().replace('[', '').replace(']', '') for x in content]
推荐阅读
- android - 将 Circleimageview 的状态保存在片段内
- java - 发布约会/预订 - CORS 政策问题
- javascript - 引导模式未在 PHP 中显示
- python - 模块 'tensorflow' 没有属性 'get_default_graph' - 我不想要任何图表
- javascript - React - 导出上下文提供程序给我错误:“元素类型无效:预期字符串 [...] 但得到:未定义”
- reactjs - 如何比较 2 个 fetch 数组以在 reactjs 中设置状态
- python - Django从查询集中的外键获取外键
- unity3d - 自动拖动变量
- wordpress - 如何显示 Wordpress 类别页面的自定义分类法?
- c# - 使用 IndexMany 时,Elasticsearch 在动态类型上推断 _id