-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgettags.py
64 lines (55 loc) · 1.82 KB
/
gettags.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
from bs4 import BeautifulSoup
import requests
import json
import yaml
URL = "https://nhentai.net/tags/"
def wtfcloudflare(url, method="get", useragent=None, cookie=None, data=None):
session = requests.Session()
session.headers = {
'Referer': "https://nhentai.net/login/",
'User-Agent': useragent,
'Cookie': cookie,
'Accept-Language': 'en-US,en;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
}
if method == "get":
r = session.get(url)
elif method == "post":
r = session.post(url, data=data)
return r
def get_tags():
with open('set.yaml', 'r') as f:
data = yaml.load(f, Loader=yaml.CLoader)
cookie = data["cookid"]
useragent = data["useragent"]
if cookie == "":
print("Please edit set.yaml")
exit()
now = 1
tagjson = {}
while True:
data = wtfcloudflare(f"{URL}?page={now}",
useragent=useragent, cookie=cookie)
soup = BeautifulSoup(data.text, 'html.parser')
tags = soup.find_all("a", class_='tag')
if tags == []:
break
tagnumbers = [t.get('class') for t in tags]
tagnames = [t.find('span', class_='name').get_text() for t in tags]
tagnumber = []
for i in tagnumbers:
fixnum = i[1].replace('tag-', '')
tagnumber.append(fixnum)
for i in enumerate(tagnumber):
tagjson[i[1]] = tagnames[i[0]]
print(f"page {now} done")
now += 1
if tagjson == {}:
print("something wrong with your cookie or useragent")
exit()
with open('tag.json', 'w') as f:
json.dump(tagjson, f)
print("tag.json saved")
return
if __name__ == '__main__':
get_tags()