以下是代码:
import requests, sys
class downloader(object):
def __init__(self):
self.server = 'http:/XXXXXXXX.net/'
self.target = 'http://m.XXXXXXXX.net/21/21323/'
self.names = []
self.urls = []
self.nums = 0
def get_download_url(self):
req = requests.get(url = self.target)
html = req.text
bf= BeautifulSoup(html,'lxml')
div_bf = BeautifulSoup(html,'lxml')
div = div_bf.find_all('div', class_ = 'listmain')
a_bf = BeautifulSoup(str(div[0]))
a = a_bf.find_all('a')
self.nums = len(a[15:])
for each in a[15:]:
self.names.append(each.string)
self.urls.append(self.server + each.get('href'))
def get_contents(self, target):
req = requests.get(url = target)
html = req.text
bf = BeautifulSoup(html)
texts = bf.find_all('div', class_ = 'showtxt')
texts = texts[0].text.replace('\xa0'*8,'\n\n')
return texts
def writer(self, name, path, text):
write_flag = True
with open(path, 'a', encoding='utf-8') as f:
f.write(name + '\n')
f.writelines(text)
f.write('\n\n')
if __name__ == "__main__":
dl = downloader()
dl.get_download_url()
print('开始下载:')
for i in range(dl.nums):
dl.writer(dl.names[i], '文本文档.txt', dl.get_contents(dl.urls[i]))
sys.stdout.write(" ?已下载:%.3f%%" % float(i/dl.nums) + '\r')
sys.stdout.flush()
print('下载完成')```
超出遍历范围了 可能是这里 " for each in a[15:]:"
调试方法1
import traceback
try:
func() # 你的代码
except Exception as e:
print(traceback.print_exc())
调试方法2
import pdb
pdb.set_trace()
@韆: QAQ大佬,现在显示的是
> d:\python\xx.py(39)<module>()
-> dl = downloader()
(Pdb)
@XD_X: 你已经进入pdb调试模式了, 就按 pdb 指令去验证你想要查看的变量就行了呀
https://www.cnblogs.com/xiaohai2003ly/p/8529472.html
"IndexError: list index out of range" 这个报错基本都是迭代对象遍历报出来的, 你代码就2个循环; 自己排查一下就好了
按照报错信息给的位置往回看代码, 给可能出错的地方都加上assert
应该是数组越界
我太菜了……各位大佬帮忙改改代码罢((QAQ
– XD_X 3年前