数列规律寻找 - python 爬虫 OEIS (2020.10.6更新)

福利网站:http://oeis.org

可以用来找规律

CODE

import requests
import bs4
import sys
import urllib.request
import urllib.parse
import json
import time
import random
import hashlib


rres = ''

s = input("输入数列: ")
res = requests.get("http://oeis.org/search?q=" + s + "&sort=&language=english&go=Search")
res.text

soup = bs4.BeautifulSoup(res.text, "html.parser")
try:
    titles = soup.find("td", align = "left", valign = "top").contents[3]
except Exception:
    print("没找到")
    sys.exit(0)
strt = str(titles)
for x in range(9, len(strt) + 1):
    if strt[x-5] == 'v' and strt[x-4] == 'a' and strt[x-3] == 'l' and strt[x-2] == 'i' and strt[x-1] == 'g'  and strt[x] == 'n':
        if strt[x-9] == 'f':
            print(" 规律为")
            for y in range(x + 8, len(strt) + 1):
                                
                
                
                if strt[y] == '<':
                    content=str(rres)

                    url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
                     
                    data = {}
                     
                    u = 'fanyideskweb'
                    d = content
                    f = str(int(time.time()*1000) + random.randint(1,10))
                    c = 'rY0D^0\'nM0}g5Mm1z%1G4'
                     
                    sign = hashlib.md5((u + d + f + c).encode('utf-8')).hexdigest()
                     
                    data['i'] = content
                    data['from'] = 'AUTO'
                    data['to'] = 'AUTO'
                    data['smartresult'] = 'dict'
                    data['client'] = 'fanyideskweb'
                    data['salt'] = f
                    data['sign'] = sign
                    data['doctype'] = 'json'
                    data['version'] = '2.1'
                    data['keyfrom'] = 'fanyi.web'
                    data['action'] = 'FY_BY_CL1CKBUTTON'
                    data['typoResult'] = 'true'
                     
                    data = urllib.parse.urlencode(data).encode('utf-8')
                    request = urllib.request.Request(url=url,data=data,method='POST')
                    response = urllib.request.urlopen(request)
                     
                    PPP = response.read().decode('utf-8')
                    
                    print(" 原文")
                    print("    ", end='')
                    for z in range(1, len(content)):
                        if(content[z] == ' ' and content[z-1] == ' ' and z < len(content)):
                            continue
                        print(content[z],end='')
                    print("翻译(小概率不准)")
                    print("    ", end='')
                    bo = 0
                    for z in range(0, len(PPP)):
                        def is_Chinese(word):
                            for ch in word:
                                if '\u4e00' <= ch <= '\u9fff':
                                    return True
                            return False
                        if(is_Chinese(PPP[z]) or bo == 1):
                            bo = 1
                            print(PPP[z],end='')
                        if(((PPP[z] == ' ' and PPP[z+1] == ' ') or PPP[z] == '"' or PPP[z] == '<' or PPP[z] == '_') and bo == 1):
                            bo = 0
                            continue
                    print("")
                        
                    sys.exit(0)
                rres = rres + str(strt[y])
                      

                     

ATTENTION

BeautifulSoup & Requests 模块
接着就是~ 有点慢

2020.10.6 更新日记
加上了 翻译 模块,翻译用的是 有道翻译,不过有时候会翻译出些奇怪的东西
然后增加了报错返回,不会直接返回一串常人看不懂的英文,会返回“没找到”

(心情好的话 有时间再改)

posted @ 2020-10-06 11:27  LT-Y  阅读(592)  评论(0)    收藏  举报