-
Notifications
You must be signed in to change notification settings - Fork 1
/
reviews_parsing.py
49 lines (35 loc) · 1.21 KB
/
reviews_parsing.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
# coding: utf-8
from typing import NamedTuple
import requests
from bs4 import BeautifulSoup
BUSINESS_ID = 119376854596
class CommentData(NamedTuple):
name: str
date: str
stars_count: int
text: str
def get_yandex_reviews(business_id):
url = f"https://yandex.ru/maps-reviews-widget/{business_id}?comments"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
comments = soup.find_all("div", {"class": "comment"})
comment_dicts = []
for comment in comments:
# parsing
comment_text = comment.find("p", {"class": "comment__text"}).text
comment_name = comment.find("p", {"class": "comment__name"}).text
comment_date = comment.find("p", {"class": "comment__date"}).text
comment_stars_count = len(comment.find_all(
"li", {"class": "stars-list__star"}))
# creating comment dicts
comment_dicts.append(
CommentData(
name=comment_name,
date=comment_date,
stars_count=comment_stars_count,
text=comment_text,
)
)
return comment_dicts
if __name__ == "__main__":
print(get_yandex_reviews(BUSINESS_ID))