requests+pyautogui抓取leecode题解的python代码

1,代码

代码如下(示例):

import json,requests,sys
from subprocess import *#调用call函数
def mkdoc(a,b):
    import os
    if not a:
        fp = open(b, "w") 
        return fp
        # 先创建.txt文件所在的文件夹
    elif not os.path.exists(a):
        os.makedirs(a)
# 创建test.txt文件
        fp = open(a+'/'+b, "w")
        return fp
    elif a:
        fp = open(a+'/'+b, "w")
        return fp
def ops(b,c):

    if b=='sf':
        loc = "显示在界面上的浏览器图标路径"
        scaleClick(loc)
        pyautogui.hotkey('command','t')#mac
        pyperclip.copy(c)
        pyautogui.hotkey('command','v')
        pyautogui.press('enter')
def scaleClick(img):
    location=pyautogui.locateCenterOnScreen(img,confidence=0.9)
    if location is not None:
        pyautogui.click(location.x*1680/3360,location.y*1050/2100,clicks=1,interval=0.2,duration=0.2,button="left")#
        return False
    else:
        return True
def find(path):
    b=scaleClick(path)
    while b:
        pyautogui.scroll(-5)
        # time.sleep(1)
        b=scaleClick(path)
        if not b:
            break
def leecode_py1():
    pyautogui.FAILSAFE=True#鼠标划到左上终止程序
    data = {"operationName":"profileArticles","variables":{"userSlug":"leetcode-solution","skip":0,"first":15},"query":"query profileArticles($userSlug: String!, $skip: Int, $first: Int) {\n  solutionArticles(userSlug: $userSlug, skip: $skip, first: $first) {\n    edges {\n      node {\n        title\n        slug\n        question {\n          title\n          titleSlug\n          translatedTitle\n          questionFrontendId\n          __typename\n        }\n        upvoteCount\n        topic {\n          commentCount\n          __typename\n        }\n        __typename\n      }\n      __typename\n    }\n    __typename\n  }\n}\n"}
    headers = {
        'X-CSRFToken':'Bvr7bnFC29yT8XWnAlxtNQNNnGSq6uxYq78Jo4fn8gBlVJtV0XlSFrVU5pU9H0Aw',
        'Referer':'https://leetcode-cn.com/u/leetcode-solution/',
        'Content-Type':'application/json',
        'origin':'https://leetcode-cn.com',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'
        }
    cookies = {'__cfduid':'d9ce37537c705e759f6bea15fffc9c58b1525271602',
                '_ga':'GA1.2.5783653.1525271604',
                '_gid':'GA1.2.344320119.1533189808',
                'csrftoken':'Bvr7bnFC29yT8XWnAlxtNQNNnGSq6uxYq78Jo4fn8gBlVJtV0XlSFrVU5pU9H0Aw',
                ' _gat':'1'}
    dumpJsonData = json.dumps(data)
    response = requests.post('https://leetcode-cn.com/graphql/',data = dumpJsonData, headers = headers,cookies = cookies)
    dictInfo = json.loads(response.text)
    content = dictInfo["data"]["solutionArticles"]["edges"]#[1]["node"]["slug"]
    mkdoc('输入文件夹路径','lcode1.py')
    for i in range(len(content):#列表
        full_url="https://leetcode-cn.com/problems/"+content[i]["node"]["question"]["titleSlug"]+"/solution/"+content[i]["node"]["slug"]+'/'
        #print(full_url)
        ops('sf',full_url)#也可以call函数打开浏览器,将url复制进去enter
        find('图标1路径')#截取的方法一的图标
        find('图标2路径')#截取的python字样
        pyautogui.moveRel(0,5)#下移
        find('图标3路径')#此时右上角出现复制图标
        call(["open", '文件夹路径/lcode1.py'])
        pyautogui.press('enter')
        # pyautogui.typewrite(content[i]["node"]["title"])
        pyautogui.press('enter')
        pyautogui.hotkey('command','v')
leecode_py1()

总结

原理就是复制粘贴

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值