侧边栏壁纸
  • 累计撰写 60 篇文章
  • 累计收到 10 条评论

playwright自动化浏览器 python 比selenium更稳定,更方便 微软出品

2022-12-6 / 0 评论 / 564 阅读
温馨提示:
本文最后更新于 2022-12-6,已超过半年没有更新,若内容或图片失效,请留言反馈。

比其他的自动化框架稳定,支持异步,谷歌的那个pyppterr已经不维护,也没有官方版本

先下载pip install playwright

基本使用(异步版本)单一窗口六联单号自动化查询

from playwright.async_api import async_playwright async def run(self,order): async with async_playwright() as pw: webkit = await pw.chromium.launch(headless=False) context = await webkit.new_context() # 需要创建一个 context page = await context.new_page() # 创建一个新的页面 await page.goto("http://query.customs.gov.cn/mnftq/MRoadQuery.aspx")#打开页面的链接 await asyncio.sleep(1)#打开页面的链接 await page.locator("#MRoadQueryCtrl1_txtManifestID").fill(order)#输入内容 await page.locator('//img[@id="MRoadQueryCtrl1_imgCode"]').screenshot(path="./img/code.png")#验证码识别 await asyncio.sleep(1) text=await self.img() print(text) await asyncio.sleep(1) await page.locator("#MRoadQueryCtrl1_txtCode").fill(text)#输入验证码 await asyncio.sleep(4) #登录 await page.click('#MRoadQueryCtrl1_btQuery') await asyncio.sleep(1) content=await page.content()#打印页面内容 html=etree.HTML(content) tr=html.xpath('//tr[@style!="color:White;background-color:#5D7B9D;font-weight:bold;"]') for td in tr: content=td.xpath('./td/text()') if content: print(content) if content[10]=='已放行': date=content[5].replace('/','-') # print(page.content()) await webkit.close()

评论一下?

OωO
取消