欢迎光临我要源码 ,本站所有资源仅供学习与参考,禁止用于商业用途或从事违法行为!

python教程

python百度翻译爬虫

python教程 51源码 2022-11-02 人阅读
import requests
import re
while 1 < 10:
    f = 0
    url = "https://fanyi.baidu.com/sug"
    a = input("请输入要翻译的单词或句子:")
    head = {
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.55"
    }
    dat = {
        "kw":a
    }
    b = requests.post(url,data=dat,headers = head)
    c = b.json()
    d = c['data']
    e = len(d)
    r = re.compile("{'k': '(?P<yc>.*?)', 'v': '(?P<fy>.*?)'}",re.S)
    while f < e:
        n = f + 1
        h = str(d[f])
        l = r.finditer(h)
        for m in l:
            print(str(n) + ". 原词: " + m.group("yc") + " 翻译 : " + m.group("fy"))
        f = f+1
        b.close()


标签 python爬虫