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/