def get_total_num_of_article(URL):
source_code_from_url = urllib.request.urlopen(URL)
soup = BeautifulSoup(source_code_from_url, 'lxml', from_encoding='utf-8')
total_num_of_article = soup.select('span.total_number')
num = total_num_of_article[0].string
position = num.index('(')
num = num[position:]
num = re.sub('[^0-9]', '', str(num))
num = int(num)
if(num % 15 >= 1):
page_num = int((num / 15) + 1)
else:
page_num = int(num / 15)
return page_num
page_num = get_total_num_of_article(TARGET_URL)
请教一下为什么第6行 num = total_num_of_article[0].string这一行代码总是报错list index out of range啊
有没有大神帮忙解答55
是用colab写的,也试了查到的tryexcept方法好像还是不太行
是爬取韩网新闻的代码
total_num_of_article 的长度能可能为 0,你调用 total_num_of_article[0] 前先判断一下吧。