/
Implementation of SEO algorithms3.py
66 lines (48 loc) · 2.14 KB
/
Implementation of SEO algorithms3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import requests
from bs4 import BeautifulSoup
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from collections import Counter
class SEOAlgorithm:
def __init__(self, url):
self.url = url
def get_keyword_density(self, keyword):
response = requests.get(self.url)
soup = BeautifulSoup(response.content, 'html.parser')
text = soup.get_text()
stop_words = set(stopwords.words('english'))
tokens = word_tokenize(text)
filtered_tokens = [token for token in tokens if token not in stop_words]
keyword_count = 0
for token in filtered_tokens:
if token == keyword:
keyword_count += 1
return keyword_count / len(filtered_tokens)
def get_title_tag(self):
response = requests.get(self.url)
soup = BeautifulSoup(response.content, 'html.parser')
title = soup.title.text
return title
def get_meta_description(self):
response = requests.get(self.url)
soup = BeautifulSoup(response.content, 'html.parser')
description = soup.find('meta', {'name': 'description'})['content']
return description
def get_backlinks(self):
# برای استفاده از این تابع باید API Key خود را از سایت Moz دریافت کنید
api_key = 'YOUR_API_KEY'
headers = {'Authorization': 'Bearer ' + api_key}
response = requests.get('https://api.moz.com/v1/domains/links?url=' + self.url, headers=headers)
data = response.json()
return data['links']
# مثال استفاده از کتابخانه
url = "https://www.example.com"
seo_algorithm = SEOAlgorithm(url)
keyword_density = seo_algorithm.get_keyword_density("برنامه نویسی پایتون")
print("چگالی کلمه کلیدی 'برنامه نویسی پایتون':", keyword_density)
title_tag = seo_algorithm.get_title_tag()
print("عنوان:", title_tag)
meta_description = seo_algorithm.get_meta_description()
print("توضیحات متا:", meta_description)
backlinks = seo_algorithm.get_backlinks()
print("تعداد بک لینک ها:", len(backlinks))