python - 类型错误 : XXXXX got an unexpected keyword argument 'XXXXXX'

标签 python json keyword-argument

我在运行代码时收到意外的关键字参数。来源:https://sempioneer.com/python-for-seo/how-to-extract-text-from-multiple-webpages-in-python/ 有人可以帮忙吗?谢谢

运行以下代码:

single_url = 'https://understandingdata.com/'
text = extract_text_from_single_web_page(url=single_url)
print(text)

给出以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_10260/3606377172.py in <module>
      1 single_url = 'https://understandingdata.com/'
----> 2 text = extract_text_from_single_web_page(url=single_url)
      3 print(text)

~\AppData\Local\Temp/ipykernel_10260/850098094.py in extract_text_from_single_web_page(url)
     42     try:
     43         a = trafilatura.extract(downloaded_url, json_output=True, with_metadata=True, include_comments = False,
---> 44                             date_extraction_params={'extensive_search': True, 'original_date': True})
     45     except AttributeError:
     46         a = trafilatura.extract(downloaded_url, json_output=True, with_metadata=True,

TypeError: extract() got an unexpected keyword argument 'json_output'

“extract_text_from_single_web_page(url=single_url)”的代码

def extract_text_from_single_web_page(url):
    
    downloaded_url = trafilatura.fetch_url(url)
    try:
        a = trafilatura.extract(downloaded_url, json_output=True, with_metadata=True, include_comments = False,
                            date_extraction_params={'extensive_search': True, 'original_date': True})
    except AttributeError:
        a = trafilatura.extract(downloaded_url, json_output=True, with_metadata=True,
                            date_extraction_params={'extensive_search': True, 'original_date': True})
    if a:
        json_output = json.loads(a)
        return json_output['text']
    else:
        try:
            resp = requests.get(url)
            # We will only extract the text from successful requests:
            if resp.status_code == 200:
                return beautifulsoup_extract_text_fallback(resp.content)
            else:
                # This line will handle for any failures in both the Trafilature and BeautifulSoup4 functions:
                return np.nan
        # Handling for any URLs that don't have the correct protocol
        except MissingSchema:
            return np.nan

最佳答案

正如我的评论中所建议的,最好的选择是找到一个不使用 trafilatura 的教程,因为这似乎是被破坏的东西。然而,修改这个特定的函数来避免它并使用后备是非常简单的:

def extract_text_from_single_web_page(url):
    try:
        resp = requests.get(url)
        # We will only extract the text from successful requests:
        if resp.status_code == 200:
            return beautifulsoup_extract_text_fallback(resp.content)
        else:
            # This line will handle for any failures in the BeautifulSoup4 function:
            return np.nan
    # Handling for any URLs that don't have the correct protocol
    except MissingSchema:
        return np.nan

关于python - 类型错误 : XXXXX got an unexpected keyword argument 'XXXXXX' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69906518/

相关文章:

python - 打开街道 map (pyproj)。如何解决语法问题?

java - 从 java 编写 .npy(numpy 二进制格式)

python - 二维数组的python总和如何返回列表

java - 如何为名称/值结构创建 JSON 模式?

python - 如何在 Python 2 中将位置参数视为关键字参数

python - 字典作为 kwargs ,其变量多于使用的变量

python - 无法显示图像网络服务器python

java - 大数字反序列化抛出 NumberFormatException

json - JQ JSON 解析器,连接数组的某个子项

Python 可选参数、位置参数和关键字参数