page_num = [item.text for item in
self.browser.find_elements_by_xpath('//div[@class="page"]/a |//div[@class="page"]/font')] #翻页定位页码
num = int(max(page_num,default=0)) #转为int 并防止num为空报错
for i in range(num): #没有调用这个i
mid_infos = self.browser.find_elements_by_xpath('//ul[@class="gc_ul"]/li')
for info in mid_infos:
title = info.find_element_by_xpath('./h5/a').text
link_onclick = info.find_element_by_xpath('./h5/a').get_attribute('onclick')
link = "https://portal.daee.cn/portal/pro/index.jsp?proId=" + re.search("gotoPro\('(.*?)',", link_onclick).group(1)
d = {"title": title, "link": link, "pubtime": pubtime}
infos.append(d)
try:
next_page = self.browser.find_element_by_xpath('//*[@id="proListBox"]/div/font/following-sibling::a[1]')
next_page.click() #翻页
except:
break
代码中没有调用到i,如果调用到i的情况下要修改range()
因为i默认(0,1)开始
如果调用i
则for i in range(1,num+1):
....
这样就可以取到正确的num了