Skip to content

Instantly share code, notes, and snippets.

@iydon
Created May 24, 2020 18:56
Show Gist options
  • Save iydon/952d9f965ba20b006012e5ceca25cbd3 to your computer and use it in GitHub Desktop.
Save iydon/952d9f965ba20b006012e5ceca25cbd3 to your computer and use it in GitHub Desktop.
随机404网址生成
import requests
from faker import Faker
from random import randint
from rstr import xeger
F = Faker()
class Base404:
def __init__(self, *args, url=None, **kwargs):
if url is None:
url = self.random_url(*args, **kwargs)
self.url = url
self._response = None
def __str__(self):
return self.url
def __repr__(self):
return f'{self.__class__.__name__}(url={repr(self.url)})'
@property
def response(self):
if self._response is None:
self._response = self.get_response()
return self._response
def get_response(self): # editable
return requests.get(self.url)
def random_url(self, *args, **kwargs): # editable
raise NotImplementedError
def is_valid(self): # editable
raise NotImplementedError
class baidu:
class pan(Base404):
def random_url(self):
return 'https://pan.baidu.com/share/' \
f'linkshareid={randint(1e4, 1e5)}&uk={randint(1e9, 1e10)}'
def is_valid(self):
return not self.response.url.endswith('404.html')
class tieba(Base404):
def random_url(self):
return f'https://tieba.baidu.com/p/{randint(1e9, 1e10)}'
def is_valid(self):
return '很抱歉,该贴已被删除。' not in self.response.text
class wenku(Base404):
def random_url(self):
code = xeger('[a-z0-9]{40}')
return f'https://wenku.baidu.com/view/{code}.htlm'
def is_valid(self):
return '对不起,该文档已被删除,无法查看' not in t.response.text
class zhihu(Base404):
def random_url(self):
return f'https://www.zhihu.com/question/{randint(1e8, 1e9)}'
def get_response(self):
headers = {'User-Agent': F.user_agent()}
return requests.get(self.url, headers=headers)
def is_valid(self):
return self.response.status_code == 200
class zhuanlan(Base404):
def random_url(self):
return f'https://zhuanlan.zhihu.com/p/{randint(1e8, 1e9)}'
def get_response(self):
headers = {'User-Agent': F.user_agent()}
return requests.get(self.url, headers=headers)
def is_valid(self):
return self.response.status_code == 200
class qq:
class weixin:
class mp(Base404):
# not stable
def random_url(self):
url = 'https://mp.weixin.qq.com/s/'
return url + xeger(r'[a-zA-Z0-9\-_]{22}')
class archive(Base404):
def random_url(self):
url = 'https://archive.vn/'
return url + xeger(r'[a-zA-Z0-9]{5}')
class github(Base404):
def random_url(self):
f = lambda x: xeger(fr'[a-z0-9\-]{{{x}}}')
return f'https://github.com/{f(randint(5, 9))}/{f(randint(1, 9))}'
if __name__ == '__main__':
print(t := baidu.pan())
print(t := baidu.tieba())
print(t := baidu.wenku())
print(t := zhihu())
print(t := zhihu.zhuanlan())
print(t := qq.weixin.mp())
print(t := archive())
print(t := github())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment