get - “模块”对象没有属性 'get' ?

标签 get python-3.4

from urllib import request
from bs4 import BeautifulSoup
def trade_spider(max_pages):
page = 1
while page <= max_pages:
    x = 16958 + int(page)
    url = 'http://mery.jp/' + str(x)
    source_code = request.get(url)
    plain_text = source_code.text
    soup = BeautifulSoup(plain_text)
    print(soup)
    break

trade_spider(2)

当我尝试在 Python3.4 中运行上面的代码时,出现以下错误:

File "web_crawler.py", line 15, in <module>
trade_spider(2)
File "web_crawler.py", line 9, in trade_spider
source_code = request.get(url)
AttributeError: 'module' object has no attribute 'get'

关于如何解决这个问题有什么想法吗?预先感谢您。

最佳答案

您从 urllib 导入了 request,但 urllib.request 没有 get 方法。我认为您想导入 requests 模块并使用 requests.get 代替 request.get。或者您想将 get 替换为 urlopen。考虑到您在下一行中引用了 .text 属性,它可能是前者而不是后者。

关于get - “模块”对象没有属性 'get' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33470585/

相关文章:

python - CherryPy 缓存如何工作?

jquery 获取 url 参数(如果存在)

angularjs http远程api加载失败

python 3 : How can object be instance of type?

python - 如何创建带参数的 GET 请求?

C#,如何只获取GET参数?

python - 使用 virtualenv 安装 django1.7 和 Python 3.4

python - 在 pygame 中使方 block 从线上弹起

python - pyautogui.locateCenterOnScreen() 返回 None 而不是坐标

python - 从 python 调用 BCP 抛出超时异常,然后立即完成