-
Notifications
You must be signed in to change notification settings - Fork 0
/
otptags.py
46 lines (36 loc) · 1.22 KB
/
otptags.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
import re
from bs4 import BeautifulSoup
import os.path
from datetime import date
import requests
import csv
import time
delay = 6
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
filename = 'ao3tags.csv'
filepath = os.path.join(BASE_DIR, filename)
def tagcount():
urls = [] # list of link to your otp tags
today = date.today()
counts = [today]
for url in urls:
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
# works count for a tag
heading = soup.find('h2',{'class':'heading'}).get_text()
regex1 = r'(?=(\d+) Works)'
count = int(re.findall(regex1, heading)[0])
counts.append(count)
# number of works with a certain age rating ranging from
# Teen and up, General, Not rated, Mature, Explicit
regex2 = r'(?=\((\d+)\))'
rating = soup.find('dd',{'id':'include_rating_tags'}).get_text()
ratings = re.findall(regex2, rating)
counts.append(', '.join(ratings))
time.sleep(delay)
data = [counts]
with open(filepath, 'a') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerows(data)
if __name__ == '__main__':
tagcount()