bonrix commited on
Commit
adf1eae
1 Parent(s): ae52287

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from bs4 import BeautifulSoup
3
+ import xml.etree.ElementTree as ET
4
+ import openpyxl
5
+ import gradio as gr
6
+
7
+ def fetch_page_info(url):
8
+ response = requests.get(url)
9
+ if response.status_code == 200:
10
+ soup = BeautifulSoup(response.text, 'html.parser')
11
+ title = soup.find('title').get_text() if soup.find('title') else 'No title found'
12
+ keywords = soup.find('meta', {'name': 'keywords'})
13
+ keywords = keywords.get('content') if keywords else 'No keywords found'
14
+ description = soup.find('meta', {'name': 'description'})
15
+ description = description.get('content') if description else 'No description found'
16
+ return title, keywords, description
17
+ return None, None, None
18
+
19
+ def main_page(sitemap_url):
20
+ excel_file = None
21
+ if sitemap_url:
22
+ response = requests.get(sitemap_url)
23
+ if response.status_code == 200:
24
+ root = ET.fromstring(response.content)
25
+
26
+ title_to_urls = {} # Dictionary to store URLs grouped by title
27
+
28
+ for url_element in root.findall(".//{http://www.sitemaps.org/schemas/sitemap/0.9}url/{http://www.sitemaps.org/schemas/sitemap/0.9}loc"):
29
+ url = url_element.text
30
+ title, _, _ = fetch_page_info(url) # Fetch only title for comparison
31
+
32
+ if title in title_to_urls:
33
+ title_to_urls[title].append(url)
34
+ else:
35
+ title_to_urls[title] = [url]
36
+
37
+ workbook = openpyxl.Workbook()
38
+ sheet = workbook.active
39
+ sheet.append(["URL", "Title", "Keywords", "Description"])
40
+
41
+ for title, urls in title_to_urls.items():
42
+ if len(urls) > 1: # Only consider titles with multiple URLs
43
+ for url in urls:
44
+ fetched_title, keywords, description = fetch_page_info(url)
45
+ sheet.append([url, fetched_title, keywords, description])
46
+
47
+ excel_file = "duplicate_titles.xlsx"
48
+ workbook.save(excel_file)
49
+
50
+ return excel_file
51
+
52
+ iface = gr.Interface(
53
+ fn=main_page,
54
+ inputs=[gr.inputs.Textbox(placeholder="Enter sitemap URL here")],
55
+ outputs="file",
56
+ live=True,
57
+ title="Duplicate Titles Finder and Excel Exporter",
58
+ description="Enter a sitemap URL to find duplicate titles and export the results to an Excel file.",
59
+ examples=[["http://www.embedded-innovations.com/sitemap.xml"]]
60
+ )
61
+
62
+ if __name__ == "__main__":
63
+ iface.launch()