نقشه های سایت XML برای کشف URL ها توسط گوگل مفید هستند. برخی از ابزارهای رایگان به شما امکان می دهند نقشه سایت XML ایجاد کنید، اما معمولاً محدودیتی به 500 URL یا بیشتر دارند. سایر ابزارهای پولی می توانند به شما در ایجاد یک نقشه سایت بسیار بزرگ کمک کنند. پایتون می تواند به شما در ایجاد نقشه سایت XML برای سئو کمک کند.
در این پست به شما نشان خواهم داد که چگونه با استفاده از پایتون یک فایل sitemap.xml بسازید و آن را به فایل هایی با کمتر از 50000 URL تقسیم کنید.
من نقشههای سایت XML را نیز Gzip میکنم، زیرا نقشههای سایت با 50000 ردیف میتواند بسیار سنگین باشد و از آنجایی که Google ظرفیت پردازش نقشههای سایت فشردهشده Gzip را دارد.
بهترین گزینه برای ساخت نقشه سایت XML در پایتون
بهترین راه برای ساخت نقشه سایت XML در پایتون استفاده از متد Pandas to_xml() است.
import pandas as pd
# List of URLs
urls = [
"https://example.com/page1",
"https://example.com/page2",
"https://example.com/page3"
]
# Create a DataFrame from the list of URLs
df = pd.DataFrame(urls, columns=["URL"])
# Convert DataFrame to XML
xml_data = df.to_xml(root_name="urlset", row_name="url", xml_declaration=True)
# Print the output
print(xml_data)
# Save the XML data to a file
with open("sitemap.xml", "w") as file:
file.write(xml_data)
<?xml version='1.0' encoding='utf-8'?>
<urlset>
<url>
<index>0</index>
<URL>https://example.com/page1</URL>
</url>
<url>
<index>1</index>
<URL>https://example.com/page2</URL>
</url>
<url>
<index>2</index>
<URL>https://example.com/page3</URL>
</url>
</urlset>
با تشکر از اریک هایکن برای ایجاد آگاهی در ساخت نقشه سایت با پانداها با استفاده از توابع ()pandas to_xml.
نقشه سایت XML را با jinja2 ایجاد کنید
کد زیر ممکن است دیگر بهترین راه حل برای این کار نباشد.
یک تشکر ویژه از هملت باتیستا که به من نشان داد چگونه این کار را انجام دهم
بخوانید: سازماندهی مجدد نقشه سایت XML با پایتون برای سرگرمی و سود
می توانید کد کامل را در مخزن Github من دانلود کنید.
import pandas as pd
import os
import datetime
from jinja2 import Template
import gzip
# Import List of URLs
list_of_urls = pd.read_csv('list_of_urls.csv')
list_of_urls
# Set-Up Maximum Number of URLs (recommended max 50,000)
n = 50000
# Create New Empty Row to Store the Splitted File Number
list_of_urls.loc[:,'name'] = ''
# Split the file with the maximum number of rows specified
new_df = [list_of_urls[i:i+n] for i in range(0,list_of_urls.shape[0],n)]
# For Each File Created, add a file number to a new column of the dataframe
for i,v in enumerate(new_df):
v.loc[:,'name'] = str(v.iloc[0,1])+'_'+str(i)
print(v)
# Create a Sitemap Template to Populate
sitemap_template='''<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% for page in pages %}
<url>
<loc>{{page[1]|safe}}</loc>
<lastmod>{{page[3]}}</lastmod>
<changefreq>{{page[4]}}</changefreq>
<priority>{{page[5]}}</priority>
</url>
{% endfor %}
</urlset>'''
template = Template(sitemap_template)
# Get Today's Date to add as Lastmod
lastmod_date = datetime.datetime.now().strftime('%Y-%m-%d')
# Fill the Sitemap Template and Write File
for i in new_df: # For each URL in the list of URLs ...
i.loc[:,'lastmod'] = lastmod_date # ... add Lastmod date
i.loc[:,'changefreq'] = 'daily' # ... add changefreq
i.loc[:,'priority'] = '1.0' # ... add priority
# Render each row / column in the sitemap
sitemap_output = template.render(pages = i.itertuples())
# Create a filename for each sitemap like: sitemap_0.xml.gz, sitemap_1.xml.gz, etc.
filename = 'sitemap' + str(i.iloc[0,1]) + '.xml.gz'
# Write the File to Your Working Folder
with gzip.open(filename, 'wt') as f:
f.write(sitemap_output)
سایر راهنماهای فنی سئو با پایتون
شما اکنون نقشه های سایت XML را ایجاد کرده اید که به گروه های کمتر از 50000 URL تقسیم شده اند، با استفاده از پایتون.
استراتژیست سئو در Tripadvisor، Seek سابق (ملبورن، استرالیا). متخصص در سئو فنی. در تلاش برای سئوی برنامهریزی شده برای سازمانهای بزرگ از طریق استفاده از پایتون، R و یادگیری ماشین.