首页 > 解决方案 > 美丽的汤模块错误(html解析器)

问题描述

我使用 beautifulsoup 来查找网页上的页数,但是当我编写代码时:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import requests
import BeautifulSoup


soup = BeautifulSoup(response.text)
pages = soup.select('div.pagination a')

a = int(pages[-2].text)
print a

它给出了以下错误:

回溯(最后一次调用):文件“C:/Users/HangaarLab/Desktop/sonartik/sonartik.py”,第 13 行,在 soup = BeautifulSoup(response.text) TypeError: 'module' object is not callable

在另一台计算机上代码运行,但它给出了这个警告:用户警告:没有明确指定解析器,所以我正在使用这个系统最好的 HTML 解析器(“html.parser”)。这通常不是问题,但如果您在另一个系统或不同的虚拟环境中运行此代码,它可能会使用不同的解析器并且行为不同。导致此警告的代码位于文件 C 的第 14 行: /Users/Ar�elik/Desktop/sikayet/klo.py。要消除此警告,请将附加参数 'features="html.parser"' 传递给 BeautifulSoup 构造函数。

我需要代码在给出第一个错误的计算机上工作。我应该怎么办?

标签: pythonbeautifulsoupwebpage

解决方案


更新

import BeautifulSoup

from bs4 import BeautifulSoup

前任:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import requests
from bs4 import BeautifulSoup
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get("https://www.sikayetvar.com/onedio", headers = headers)

soup = BeautifulSoup(response.text, "html.parser")   #Use a parser to fix second error warning 
pages = soup.select('div.pagination a')

a = int(pages[-2].text)
print a

推荐阅读