gpt4 book ai didi

python - Beautiful soup 模块错误(html 解析器)

转载 作者:行者123 更新时间:2023-12-01 01:39:36 26 4
gpt4 key购买 nike

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

关于python - Beautiful soup 模块错误(html 解析器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52037382/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com