-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvisualizzazioni_youtube.py
61 lines (43 loc) · 1.57 KB
/
visualizzazioni_youtube.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
import pandas as pd
import requests
from bs4 import BeautifulSoup
import re
def Soup(url):
session = requests.Session()
session.headers[
"User-Agent"] = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"
html = session.get(url).content
return BeautifulSoup(html, "html.parser")
def Views(canzone="", artista=""):
keyword = str(artista).replace("Featuring", "").split()[0] + "-" + str(canzone)
url = "https://google.com/search?q=" + keyword + "(official video)"
soup = Soup(url)
l = soup.find_all("div", attrs={"class": "twQ0Be"})
l = str(l)
n = l.find("href")
link = l[n:].split('"')[1]
soup_y = Soup(link)
between_script_tags = re.search('viewCount(.*)"', str(soup_y))
stringa = str(between_script_tags)[str(between_script_tags).find('viewCount\\\\":'):].replace("\\", "")
n_views = stringa.split('"')[2]
return int(n_views)
def visual(df):
visual = []
for index, row in df.iterrows():
try:
t = Views(row['Titolo'], row['Artista'])
print(row['Titolo'], "-", row['Artista'], "\n", "Visualizzazioni: ", t)
visual.append(t)
print("Done")
except:
t = "Errore"
visual.append(t)
print("Errore")
df["YouTube"] = visual
return df
if __name__ == '__main__':
# PROVA
# df[0:100]
df = pd.read_csv("dataset/Dataset.csv", error_bad_lines=False, sep=',')
df = visual(df)
#df.to_csv("dataset/Dataset.csv", index=False, encoding='utf-8')