漂亮的汤模块错误(html 解析器)

Beautiful soup module error(html parser)

我使用 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

它给出了以下错误:

Traceback (most recent call last): File "C:/Users/HangaarLab/Desktop/sonartik/sonartik.py", line 13, in soup = BeautifulSoup(response.text) TypeError: 'module' object is not callable

In another computer the code runs but it gives this warning: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html.parser"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.The code that caused this warning is on line 14 of the file C:/Users/Ar�elik/Desktop/sikayet/klo.py. To get rid of this warning, pass the additional argument 'features="html.parser"' to the BeautifulSoup constructor.

我需要在出现第一个错误的计算机上运行的代码。我该怎么办?

更新

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

您必须从 bs4 包

导入BeautifulSoup
import urllib2
import requests
from bs4 import BeautifulSoup #here
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get("https://www.sikayetvar.com/onedio", headers = headers)

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

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

BeautifulSoup 是 bs4 包的一部分。要修复您的代码,只需执行以下操作:

pip install bs4 

在命令行中将导入更改为:

from bs4 import BeautifulSoup