line20涉及代码:
def scrach(url):
html=requests.get(url)
selector=etree.HTML(html.text)
webb=selector.xpath('/html/body')
dict_1={}
for info in webb:
webb_name=selector.xpath('//[@id="j_p_postlist"]')
author=webb_name['webb_name']['author_n']
web_content=selector.xpath('//[@id="post_content_88796553069"]/text()')
content=web_content['web_content']['content_n']
print(author)
print(content)
dict_1['author_n']=author
dict_1['content_n']=content
documents(dict_1)
终端报错内容:
Traceback (most recent call last):
File "c:/Users/Administrator.DESKTOP-MP1O82P/Desktop/py/scrach/Xpath-final.py", line 43, in
results=pool.map(scrach,page)
File "C:\Users\Administrator.DESKTOP-MP1O82P\AppData\Local\Programs\Python\Python38\lib\multiprocessing\pool.py", line 364, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "C:\Users\Administrator.DESKTOP-MP1O82P\AppData\Local\Programs\Python\Python38\lib\multiprocessing\pool.py", line 771, in get
raise self._value
File "C:\Users\Administrator.DESKTOP-MP1O82P\AppData\Local\Programs\Python\Python38\lib\multiprocessing\pool.py", line 125, in worker
result = (True, func(*args, **kwds))
File "C:\Users\Administrator.DESKTOP-MP1O82P\AppData\Local\Programs\Python\Python38\lib\multiprocessing\pool.py", line 48, in mapstar
return list(map(*args))
File "c:/Users/Administrator.DESKTOP-MP1O82P/Desktop/py/scrach/Xpath-final.py", line 20, in scrach
author=webb_name['webb_name']['author_n']
TypeError: list indices must be integers or slices, not str
这样的报错是什么意思?该如何解决?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- honestman_ 2022-09-22 18:38关注
TypeError: list indices must be integers or slices, not str
报错原因是 列表的索引必须是int类型,你给的是字符串
解决方法:检查一下webb_name是个列表 还是webb_name['webb_name']是个列表,用索引取列表值本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 做个有关计算的小程序
- ¥15 MPI读取tif文件无法正常给各进程分配路径
- ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
- ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
- ¥15 setInterval 页面闪烁,怎么解决
- ¥15 如何让企业微信机器人实现消息汇总整合
- ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
- ¥15 如何用Python爬取各高校教师公开的教育和工作经历
- ¥15 TLE9879QXA40 电机驱动
- ¥20 对于工程问题的非线性数学模型进行线性化