gpt4 book ai didi

python - BeautifulSoup4 with Python3 - 如何使用规则将输出数据分离并写入不同的文件中?

转载 作者:太空宇宙 更新时间:2023-11-03 15:33:30 25 4
gpt4 key购买 nike

这是我的代码:

from bs4 import BeautifulSoup
import requests

getUrl= 'https://ta.wikipedia.org/wiki/அலெக்சா இணையம்'
url = getUrl
content = requests.get(url).content
soup = BeautifulSoup(content,'lxml')
heading = soup.title
refError = soup.findAll ('span', { 'class' : "error mw-ext-cite-error"})
for error in refError:
err_str = str(error)
err_str=err_str.replace("<span", heading.text+"~ <span").replace(" - தமிழ் விக்கிப்பீடியா", "")
print(err_str)

这是我的输出数据,以页面名称~ <span>开头 并以 </span>. 结尾

例如,(记住这是一行)

அல்த்தாய் பிரதேசம்~ <span class="error mw-ext-cite-error" dir="ltr"
lang="ta" xml:lang="ta">பிழை காட்டு: Invalid <code>&lt;ref&gt;</code>
tag; name "2010Census" defined multiple times with different
content</span> Before this closing tag </span>

输出数据末尾始终有一条引用错误消息,该消息根据维基百科页面的不同而有所不同。

  1. 未在之前的文本中使用。 </span>

  • குறிச்சொல்லுக்குஉரையேதும்வழங்கப்படவில்ல ை </span>
  • 使用不同内容多次定义 </span>
  • 如果我运行此代码 1000 个 getUrl(页面名称),我将获得 1000 个输出数据。现在我想将具有相同错误消息的页面分组在 .txt 中文件?就像下面这样,

    1. 带有引用错误消息的页面 -->未在之前的文本中使用.txt
    2. 带有引用错误消息的页面 -->குறிச்சொல்லுக்கு உரையேதும் வழங்கப்பட வில்லை.txt
    3. 带有引用错误消息的页面 -->使用不同内容多次定义 </span> .txt

    如何?

    最佳答案

    这是解决该问题的一种方法。请尝试以下源代码:

    from bs4 import BeautifulSoup
    import requests
    import re

    # Url of the webpage to be scraped
    getUrl= 'https://ta.wikipedia.org/wiki/அலெக்சா இணையம்'
    url = getUrl
    content = requests.get(url).content

    # Patterns to be checked
    pattern1 = re.compile(r'not used in prior text')
    pattern2 = re.compile(r'குறிச்சொல்லுக்கு உரையேதும் வழங்கப்படவில்லை')
    pattern3 = re.compile(r'defined multiple times with different content')

    # Respective Error files
    error_file1 = open("not_used_in_prior_text.txt", "w", encoding="utf-8")
    error_file2 = open("குறிச்சொல்லுக்கு_உரையேதும்_வழங்கப்படவில்லை.txt", "w", encoding = "utf-8")
    error_file3 = open("defined_multiple_times_with_different_content.txt", "w", encoding = "utf-8")
    error_file4 = open("Anomalous_Errors.txt","w", encoding = "utf-8")

    soup = BeautifulSoup(content,'lxml')
    heading = soup.title
    refError = soup.findAll ('span', { 'class' : "error mw-ext-cite-error"})

    # Check for error patterns and save it in respective files
    for error in refError:
    err_str = str(error)
    err_str=err_str.replace("<span", heading.text+"~ <span").replace(" - தமிழ் விக்கிப்பீடியா", "")
    if pattern1.search(err_str):
    error_file1.write(err_str)
    elif pattern2.search(err_str):
    error_file2.write(err_str)
    elif pattern3.search(err_str):
    error_file3.write(err_str)
    else:
    error_file4.write(err_str)
    print(err_str)

    # Close the files
    error_file1.close()
    error_file2.close()
    error_file3.close()
    error_file4.close()

    编辑源代码2

    from bs4 import BeautifulSoup 
    import requests
    import re

    # Url of the webpage to be scraped
    getUrl= 'https://ta.wikipedia.org/wiki/அலெக்சா இணையம்'
    url = getUrl
    content = requests.get(url).content

    # Patterns to be checked
    pattern1 = re.compile(r'not used in prior text')
    pattern2 = re.compile(r'குறிச்சொல்லுக்கு உரையேதும் வழங்கப்படவில்லை')
    pattern3 = re.compile(r'defined multiple times with different content')

    # Respective Error files
    error_file1 = open("not_used_in_prior_text.txt", "w", encoding="utf-8")
    error_file2 = open("குறிச்சொல்லுக்கு_உரையேதும்_வழங்கப்படவில்லை.txt", "w", encoding = "utf-8")
    error_file3 = open("defined_multiple_times_with_different_content.txt", "w", encoding = "utf-8")
    error_file4 = open("Anomalous_Errors.txt","w", encoding = "utf-8")

    soup = BeautifulSoup(content,'lxml')

    heading = soup.title.text
    heading = heading.replace(" - தமிழ் விக்கிப்பீடியா", "")
    print(heading) # you can comment this line out
    refError = soup.findAll ('span', { 'class' : "error mw-ext-cite-error"})

    # Check for error patterns and save it in respective files
    for error in refError:
    err_str = error.text
    print_error = heading+" ~ "+err_str
    if pattern1.search(err_str):
    error_file1.write(print_error)
    elif pattern2.search(err_str):
    error_file2.write(print_error)
    elif pattern3.search(err_str):
    error_file3.write(print_error)
    else:
    error_file4.write(print_error)
    print(print_error)

    # Close the files
    error_file1.close()
    error_file2.close()
    error_file3.close()
    error_file4.close()

    关于python - BeautifulSoup4 with Python3 - 如何使用规则将输出数据分离并写入不同的文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42726699/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com