get_urls() got an unexpected keyword argument 'urls'怎么改
时间: 2023-09-29 19:09:49 浏览: 31
这个错误通常是因为你的函数参数列表中没有定义 `urls` 这个关键字参数,但是你在函数调用时却使用了 `urls` 这个参数。你可以尝试在函数定义时添加 `**kwargs` 来接受所有额外的关键字参数,例如:
```python
def get_urls(**kwargs):
# 这里的代码处理你需要的参数
pass
```
这样就可以接受任意的关键字参数了。如果你需要在函数中使用 `urls` 这个参数,可以使用 `kwargs.get('urls')` 来获取它。
相关问题
TypeError: get_urls() got an unexpected keyword argument 'url'
这个错误通常表示你在调用函数 `get_urls()` 的时候,传入了一个不该传入的关键字参数 `url`。可能是因为你的代码中写错了函数的参数名,或者是因为你使用了一个过时的函数或库,导致参数名发生了变化。
解决这个问题的方法很简单,只需要检查一下你的代码,确认传入的参数名是否正确,或者尝试更新相关的函数或库,就可以了。如果还有问题,可以提供更多的上下文信息,我会尽力帮助你解决。
Traceback (most recent call last): File "C:/Users/Administrator/PycharmProjects/pythonProject/main.py", line 56, in <module> get_urls(urls='http://www.offcn.com/shizheng/sshz/') TypeError: get_urls() got an unexpected keyword argument 'urls'
这个错误提示说明在调用函数 `get_urls()` 时,使用了错误的参数名 `urls`,导致函数无法识别这个参数。从提示信息中可以看出,函数定义中并没有包含名为 `urls` 的参数。
解决这个问题的方法是修改函数调用中的参数名,将 `urls` 修改为 `url`,即将参数名改为函数定义中所使用的参数名。修改后的代码如下:
```python
import requests
from bs4 import BeautifulSoup
def get_urls(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
links = []
for link in soup.find_all('a'):
href = link.get('href')
if href is not None:
links.append(href)
return links
# 调用函数
urls = get_urls(url='http://www.offcn.com/shizheng/sshz/')
print(urls)
```
这样就可以正常调用函数并获取链接了。需要注意的是,函数定义中只有一个参数 `url`,所以在调用函数时,只需要传入一个参数并使用参数名 `url` 即可。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)