-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
81 lines (59 loc) · 2.28 KB
/
utils.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
69
70
71
72
73
74
75
76
77
78
79
80
81
import re
from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
import json
import requests
def get_view_count(url):
"""
:param url: URL of the video
:return: Video views count
"""
req = Request(url)
content = urlopen(req).read().decode('utf8')
soup = BeautifulSoup(content, 'lxml')
tag = soup.find_all('script', text=re.compile("viewCount"))
infos = tag[0].get_text().replace(' ', '').split('=', 1)[1].replace(';', '')
json_object = json.loads(infos)
view_count = json_object['videoDetails']['viewCount']
return view_count
def get_video_id(url):
"""
:param url: URL of the video
:return: Id of the video
"""
req = Request(url)
content = urlopen(req).read().decode('utf8')
soup = BeautifulSoup(content, 'lxml')
tag = soup.find_all('script', text=re.compile("viewCount"))
infos = tag[0].get_text().replace(' ', '').split('=', 1)[1].replace(';', '')
json_object = json.loads(infos)
video_id = json_object['videoDetails']['videoId']
return video_id
def get_like_count(url):
"""
:param url: URL of the video
:return: Video likes count
"""
# proxies = {'http': '20.105.253.176'}
req = Request(url)
content = urlopen(req).read().decode('utf8')
# content = requests.get(url, proxies=proxies).text
soup = BeautifulSoup(content, 'lxml')
tag = soup.find_all('script', text=re.compile("J'aime"))
infos = tag[0].get_text().replace(' ', '').split('=', 1)[1].replace(';', '').replace(u"\u2022", "*")
json_object = json.loads(infos)
like_infos = json_object['contents']['twoColumnWatchNextResults']['results']['results']['contents'][0]\
['videoPrimaryInfoRenderer']['videoActions']['menuRenderer']['topLevelButtons'][0]['toggleButtonRenderer']\
['defaultText']['accessibility']['accessibilityData']['label']
extract = like_infos.encode("ascii", "ignore")
like_count = extract.decode().split('clic')[0]
return like_count
def get_http_proxy():
url = 'https://sslproxies.org/'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'lxml')
for tr in soup.find_all('tr', class_=False):
current = []
for td in tr.find_all('td'):
current.append(td.get_text())
print(current)