列表超出范围12

时间:2017-03-17 09:09:47

标签: python web-scraping beautifulsoup

我想使用BeautifulSoup4来获取BBS的总页数。所以我写了这段代码:

pagenumber = soup.find_all("div",class_="pagelinks")[0]
page_toatal_num = pagenumber.find_all("a")[-1].get_text()
print page_toatal_num

页面的元素在这里: enter image description here

在这张照片中,我希望得到“342”,按照我的方式,我得到了它。但是,令人困惑的是,它引发了错误:“列表索引超出范围”。在cmd.exe中,它既打印“342”又引发错误。为什么?我怎样才能解决这个问题

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\scrapy\utils\defer.py", line 102, in iter_
errback
    yield next(it)
  File "C:\Python27\lib\site-packages\scrapy\spidermiddlewares\offsite.py", line
 29, in process_spider_output
    for x in result:
  File "C:\Python27\lib\site-packages\scrapy\spidermiddlewares\referer.py", line
 22, in <genexpr>
    return (_set_referer(r) for r in result or ())
  File "C:\Python27\lib\site-packages\scrapy\spidermiddlewares\urllength.py", li
ne 37, in <genexpr>
    return (r for r in result or () if _filter(r))
  File "C:\Python27\lib\site-packages\scrapy\spidermiddlewares\depth.py", line 5
8, in <genexpr>
    return (r for r in result or () if _filter(r))
  File "C:\python_workspace\deep_web\deep_web\spiders\deepSpider.py", line 73, i
n parse_item
    page_toatal_num = pagenumber.find_all("a")[-1].get_text()
IndexError: list index out of range

0 个答案:

没有答案