Python Forum
Copy xml content from webpage and save to locally without special characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Copy xml content from webpage and save to locally without special characters
#9
Quote:My html file has a few lines of PHP at the top, but is mainly html.

The output opens as .xml in my browser, not as a webpage
If the .xml it's genrated it will be a string.
Then need to pass that string to BS,and not the url.
Again there is nothing i can run ans test with what you post.
from bs4 import BeautifulSoup

# Your XML content as a string
xml_content = """
<?xml version='1.0' encoding='UTF-8'?><IM415 xmlns="http://www.ros.ie/schemas/customs/IM415">
  <Declaration>
    <MsgType>H1</MsgType>
    <DeclarationType_1_1>IM</DeclarationType_1_1>
    <AdditionalDeclarationType_1_2>A</AdditionalDeclarationType_1_2>
    <LRN_2_5>NIK243104_16nUlp</LRN_2_5>
    <ValuationInformation>
      <InvoiceCurrency_4_10>AFA</InvoiceCurrency_4_10>
      <InvoiceAmount_4_11>5000</InvoiceAmount_4_11>
      <InternalCurrency_4_12>AFA</InternalCurrency_4_12>
    </ValuationInformation>
    <GoodsInformation>
      <GrossMass_6_5>33300</GrossMass_6_5>
      <TotalPackageNumber_6_18>1665</TotalPackageNumber_6_18>
    </GoodsInformation>
</Declaration>
</IM415>
"""

# Now use lxml as parser
soup = BeautifulSoup(xml_content, 'lxml')
print(soup.prettify())

# Save the modified XML to a file
with open('test.xml', 'w', encoding='utf-8') as fp:
    fp.write(soup.prettify())
Reply


Messages In This Thread
RE: Copy xml content from webpage and save to locally without special characters - by snippsat - Mar-22-2024, 04:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Locally run an APK and execute functions using Python KovyJ 0 496 Jan-23-2025, 05:21 PM
Last Post: KovyJ
  [SOLVED] Special characters in XML ForeverNoob 3 1,847 Dec-04-2024, 01:26 PM
Last Post: ForeverNoob
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 1,378 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  how to save to multiple locations during save cubangt 1 1,339 Oct-23-2023, 10:16 PM
Last Post: deanhystad
Question Special Characters read-write Prisonfeed 1 1,488 Sep-17-2023, 08:26 PM
Last Post: Gribouillis
  UPDATE SQLITE TABLE - Copy a fields content to another field. andrewarles 14 6,588 May-08-2021, 04:58 PM
Last Post: ibreeden
  Rename Multiple files in directory to remove special characters nyawadasi 9 10,749 Feb-16-2021, 09:49 PM
Last Post: BashBedlam
  copy content of text file with three delimiter into excel sheet vinaykumar 0 2,946 Jul-12-2020, 01:27 PM
Last Post: vinaykumar
  Remove escape characters / Unicode characters from string DreamingInsanity 5 22,021 May-15-2020, 01:37 PM
Last Post: snippsat
  Check for a special characters in a column and flag it ayomayam 0 2,617 Feb-12-2020, 03:04 PM
Last Post: ayomayam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020