module 'urllib.request' has no attribute 'get'
时间: 2023-09-30 20:03:00 浏览: 271
该错误是因为在引用urllib库时,使用了错误的导入方式。在Python 3中,urllib库已经被拆分为多个子模块,其中request模块包含了get方法。根据引用和的信息,正确的导入方式应该是使用`import urllib.request`。在您的代码中,使用`urllib.request.Request`来创建请求对象,然后调用`urlopen()`方法发送请求并获取响应数据。修改您的代码如下:
```python
import urllib.request
def load_page(url, filename):
headers = {"User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;)"}
request = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(request)
return response.read()
```
这样修改后,您就可以成功使用`get`方法进行请求了。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [【python初级】 module “urllib“ has no attribute “request](https://blog.csdn.net/jn10010537/article/details/112068443)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [Python3中urllib请求网页报错(AttributeError: module ‘urllib‘ has no attribute ‘request‘)](https://blog.csdn.net/weixin_55374431/article/details/126969583)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文