Python Forum
Need help implmenting if/else or case statements for option to choose file format.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help implmenting if/else or case statements for option to choose file format.
#1
How can I implement an if/else or case statement for choosing a file format of my choice?
def main():
   parser = argparse.ArgumentParser(description="Link Extractor Tool with Python")
   parser.add_argument("url", help="The URL to extract links from.")
   parser.add_argument("-m", "--max-urls", help="Number of max URLs to crawl, default is 30.", default=30, type=int)
   args = parser.parse_args()
   url = args.url
   max_urls = args.max_urls
   domain_name = urlparse(url).netloc
   res = requests.get(url)
   statuscode = res.status_code
   print("Status Code:", statuscode)
   if statuscode == 200: 
      crawl(url, max_urls=max_urls)
   else:
      print("Failed to get a request response back.")

   print("Total Internal Links:", len(internal_links))
   print("Total External Links:", len(external_links))
   print("Total Links:", len(external_links) + len(internal_links))

   with open(f"{domain_name}_internal_links.txt", "w") as f:
      for internal_link in internal_links:
         print(internal_link.strip(), file=f)
   with open(f"{domain_name}_external_links.txt", "w") as f:
      for external_link in external_links:
         print(external_link.strip(), file=f)

   #writing to json files
   f = open(f"{domain_name}_internal_links.json","w")
   json.dump({'internal_links':list(internal_links)}, f, indent=6)
   f.close()
   f = open(f"{domain_name}_external_links.json","w")
   json.dump({'external_links':list(external_links)}, f, indent=6)
   f.close()

   #writing to csv
   df = pd.DataFrame(list(internal_links))
   df.to_csv(f"{domain_name}_internal_links.csv", index=False, header=False)
   df = pd.DataFrame(list(external_links))
   df.to_csv(f"{domain_name}_external_links.csv", index=False, header=False)

   #writing to xml
   xmlformat = xml.Element("internal_links")
   xmlformat_1 = xml.SubElement(xmlformat, "link")
   for l in list(internal_links):
      xmlformat_1.text = str(l)
      xmlformat.append(xmlformat_1)
   tree = xml.ElementTree(xmlformat)
   tree.write(f"{domain_name}_internal_links.xml")

   xmlformat = xml.Element("external_links")
   xmlformat_1 = xml.SubElement(xmlformat, "link")
   for l in list(external_links):
      xmlformat_1.text = str(l)
      xmlformat.append(xmlformat_1)
   tree = xml.ElementTree(xmlformat)
   tree.write(f"{domain_name}_external_links.xml")

#executing the python script
if __name__ == "__main__":
   main()
Reply
#2
this script is incomplete and cannot be run as posted.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  which Python choose bernd22 1 761 Sep-13-2024, 12:50 PM
Last Post: buran
  Which method name would you choose? Gribouillis 7 1,677 May-30-2024, 07:05 AM
Last Post: buran
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 1,601 Feb-15-2023, 05:34 PM
Last Post: zsousa
  Switch case or match case? Frankduc 9 7,347 Jan-20-2022, 01:56 PM
Last Post: Frankduc
  Mac os choose file name howard687 1 2,483 Jan-05-2022, 06:54 AM
Last Post: Larz60+
  Reshape txt file into particular format using python shantanu97 0 2,041 Dec-10-2021, 11:44 AM
Last Post: shantanu97
  Loop with choose Irv1n 5 4,262 Sep-16-2021, 09:31 PM
Last Post: deanhystad
  How can we transcode encoding file uml url format Anldra12 9 4,754 Jul-25-2021, 09:30 AM
Last Post: Anldra12
  How to design a save file format? philipbergwerf 5 5,381 Apr-26-2021, 07:39 PM
Last Post: Gribouillis
  logger option , where is the file? korenron 1 2,451 Apr-25-2021, 01:28 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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