运行报错:dumppy line 42 in module response_text = response_textresponse_textindex~+1response_textindex~1ValueError substring not found下面的代码:import requestsimport random# 随机User-Agentuser_agents = M
这个错误是由于在代码的第42行中,response_text.index('~')没有找到'~'字符串,导致substring not found的错误。
要解决这个问题,你可以在使用index()方法之前先检查字符串中是否包含'~'字符。可以使用'~' in response_text来检查。如果包含,再执行response_text.index('~')+1:response_text.index('~1')。
你可以将代码修改为以下内容:
if '~' in response_text:
response_text = response_text[response_text.index('~')+1:response_text.index('~1')]
f.write("Data for {}: {}\n".format(column_name, response_text))
else:
f.write("Data for {}: {}\n".format(column_name, "Substring not found"))
这样,如果字符串中包含'~'字符,就会执行正确的操作,否则会写入一个提示信息
原文地址: https://www.cveoy.top/t/topic/ihAQ 著作权归作者所有。请勿转载和采集!