Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- # A CGI script that redirects the user to a random Mainchan subchan.
- # Note: This script may be slow and wasteful of data because the subchan
- # list is scraped each time this script runs.
- import random
- import requests
- from bs4 import BeautifulSoup
- def getSubList():
- sublist = []
- headers = {'User-Agent': 'mainchanrandom scraper'}
- n = 1
- while n < 10: # the n < 10 check is for the loop to not iterate forever just incase the 'no links in table' check will fail
- url = f'https://mainchan.com/subchan-list?page={n}'
- sublist_page = requests.get(url, headers=headers)
- soup = BeautifulSoup(sublist_page.content, "html.parser")
- sublist_table = soup.find("table", class_="sublist-table") # finds first occurence of table tag
- sublinks = sublist_table.find_all("a")
- if not sublinks: # if there are no links in the table, then we have reached the end of the pages
- break
- for link in sublinks:
- sublist.append(link["href"])
- n = n + 1
- return sublist
- sublist = getSubList()
- randomsub_url = sublist[random.randint(0, len(sublist)-1)]
- print("Content-Type: text/html")
- print(f"Location: {randomsub_url}")
- print("\r\n\r\n", end='')
- print("<html>")
- print(" <head>")
- print(f" <meta http-equiv='refresh' content='0;url={randomsub_url}'/>")
- print(" <title>Redirecting...</title>")
- print(" </head>")
- print(" <body>")
- print(" <center>")
- print(f" <p>Redirecting... <br><a href={randomsub_url}>click here</a> if it doesn't work</p>")
- print(" </center>")
- print(" </body>")
- print("</html>")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement