实例:在线工具接口解析
python
import requests
from parsel import Selector
import re
links = [{'link': 'https://c.runoob.com/compile/6208/', 'label': 'Groovy'}, {'link': 'https://c.runoob.com/compile/6206/', 'label': 'Assembly'}, {'link': 'https://c.runoob.com/compile/5649/', 'label': 'R'}, {'link': 'https://c.runoob.com/compile/5648/', 'label': 'VB.NET'}, {'link': 'https://c.runoob.com/compile/5577/', 'label': 'TypeScript'}, {'link': 'https://c.runoob.com/compile/2960/', 'label': 'Kotlin'}, {'link': 'https://c.runoob.com/compile/73/', 'label': 'Pascal'}, {'link': 'https://c.runoob.com/compile/66/', 'label': 'Lua'}, {'link': 'https://c.runoob.com/compile/22/', 'label': 'Node.js'}, {'link': 'https://c.runoob.com/compile/21/', 'label': 'Go'}, {'link': 'https://c.runoob.com/compile/20/', 'label': 'Swift'}, {'link': 'https://c.runoob.com/compile/19/', 'label': 'RUST'}, {'link': 'https://c.runoob.com/compile/18/', 'label': 'Bash'}, {'link': 'https://c.runoob.com/compile/17/', 'label': 'Perl'}, {'link': 'https://c.runoob.com/compile/16/', 'label': 'Erlang'}, {'link': 'https://c.runoob.com/compile/15/', 'label': 'Scala'}, {'link': 'https://c.runoob.com/compile/14/', 'label': 'C#'}, {'link': 'https://c.runoob.com/compile/13/', 'label': 'Ruby'}, {'link': 'https://c.runoob.com/compile/12/', 'label': 'C++'}, {'link': 'https://c.runoob.com/compile/11/', 'label': 'C'}, {'link': 'https://c.runoob.com/compile/10/', 'label': 'Java'}, {'link': 'https://c.runoob.com/compile/9/', 'label': 'Python3'}, {'link': 'https://c.runoob.com/compile/6/', 'label': 'Python2'}, {'link': 'https://c.runoob.com/compile/1/', 'label': 'PHP'}]
# ops = selector.xpath('//*[@id="sel1"]/option')
# l = []
# for o in ops:
# l.append(dict(link=o.xpath('@value').get(),
# label=o.xpath('text()').get().strip(' 在线工具')))
# print(l)
l = []
for item in links:
html = requests.get(item['link']).content.decode()
selector = Selector(text=html)
code = selector.xpath('//*[@id="code"]').get()
code = re.search(r"<textarea.*?>(.*?)</textarea>",code, re.S).group(1)
token = re.search("token = '(.*?)';", html).group(1)
runcode = int(re.search("runcode = (.*?);", html).group(1))
fileext = re.search('fileext:"(.*?)"', html).group(1)
mode = re.search('mode: "(.*?)"', html).group(1)
d = dict(label=item['label'],token=token,value=runcode,fileext=fileext,code=code,mode=mode)
l.append(d)
import json
print(json.dumps(l, indent=2, ensure_ascii=False))