BeautifulSoup有时不能正确解析数据

    工作中碰到一个问题,有一个字段需要做抓取

    例如 想获取网页http://tools.2345.com/frame/black/list/1?p=1中总共的页码

    通过BeautifuleSoup可以这样获取

resp = requests.get('http://tools.2345.com/frame/black/list/1?p=1')
soup = BeautifulSoup(resp.text, 'lxml')
elements = soup.find('div', class_='page')
print(elements)

    结果打印的结果为None

    很是无语,上网查到可能的原因是处理的文档太大,而处理的解析器缓存不够造成的信息丢失。

    换一个解析器,使用html.parser就可以解析出正确的结果


猜你喜欢

转载自blog.csdn.net/hsc_1/article/details/81016353