AttributeError: module 'urllib' has no attribute 'Request'
时间: 2023-08-03 19:06:48 浏览: 177
关于.NET Attribute在数据校验中的应用教程
这个错误是由于在Python3中,urllib库的结构发生了变化导致的。在Python3中,urllib库被拆分为urllib.request、urllib.parse等模块。所以,你需要将代码中的urllib替换为urllib.request才能解决这个错误。具体来说,你需要将代码中的import urllib.request修改为import urllib.request,并将urllib.request.Request替换为urllib.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)
return urllib.request.urlopen(request).read()
```
这样修改后,你应该能够成功导入urllib.request并解决AttributeError: module 'urllib' has no attribute 'Request'的错误了。\[1\]\[2\]
#### 引用[.reference_title]
- *1* *2* [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^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文