python - 美丽的汤模块错误(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 构造函数。
我需要代码在给出第一个错误的计算机上工作。我应该怎么办?
解决方案
更新
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
推荐阅读
- flutter - 使用网格布局的文本和文本字段
- powerbi - 函数日期的参数有错误的数据类型或结果太小或太大错误
- excel - 将工作簿转换为 HTML 后,如何隐藏隐藏列?
- python - 在张量流中使用 1D CNN 对可变长度的时间序列进行分类
- python - 在 Python 列表理解中使用带有 if/elif 语句的“for”循环
- php - .htaccess 动态子域
- android - 加载原生 Android 库时,优先选择某些 CPU 架构
- amazon-web-services - 无法从 Spark 连接到 AWS Elasticsearch
- utf-8 - 如何检测和修复不正确的字符编码
- paypal - 贝宝沙盒账户:促进者和首要类型?