-
Notifications
You must be signed in to change notification settings - Fork 1
/
library for competitor analysis2.py
68 lines (53 loc) · 2.38 KB
/
library for competitor analysis2.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
67
68
import requests
from bs4 import BeautifulSoup
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from collections import Counter
class CompetitorAnalyzer:
def __init__(self, url):
self.url = url
def get_website_info(self):
response = requests.get(self.url)
soup = BeautifulSoup(response.content, 'html.parser')
title = soup.find('title').text
meta_description = soup.find('meta', attrs={'name': 'description'})
if meta_description:
meta_description = meta_description['content']
else:
meta_description = ''
return title, meta_description
def get_keywords(self):
title, meta_description = self.get_website_info()
text = title + ' ' + meta_description
preprocessed_text = text.lower()
preprocessed_text = text.replace('.', ' ')
preprocessed_text = text.replace(',', ' ')
preprocessed_text = text.replace(';', ' ')
preprocessed_text = text.replace('!', ' ')
preprocessed_text = text.replace('?', ' ')
preprocessed_text = text.replace('(', ' ')
preprocessed_text = text.replace(')', ' ')
preprocessed_text = text.replace('"', ' ')
preprocessed_text = text.replace("'", ' ')
tokens = word_tokenize(preprocessed_text)
stop_words = set(stopwords.words('english'))
filtered_tokens = [token for token in tokens if token not in stop_words]
keywords = Counter(filtered_tokens).most_common(10)
return keywords
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"
competitor_analyzer = CompetitorAnalyzer(url)
title, meta_description = competitor_analyzer.get_website_info()
print("عنوان:", title)
print("توضیحات متا:", meta_description)
keywords = competitor_analyzer.get_keywords()
print("کلمات کلیدی:", keywords)
backlinks = competitor_analyzer.get_backlinks()
print("تعداد بک لینک ها:", len(backlinks))