-[What is Pyppeteer](What is #pyppeteer) -Install -[How to use](# How to use) -[Launch browser and open site](# Launch browser and open site) -[Get Element](# Get Element) --[Enter in text box](# Enter in text box) --Click -[Get attribute value](# Get attribute value) --[Get innerHTML](Get #innerhtml)
Pyppeteer is a Python package for operating the Chrome browser, which is a port of the library Puppeteer for node.js to Python. Since there is little information on Pyppereer, it may be better to search with Puppeteer when searching.
Pyppeteer author's blog Puppeteer site
pip install pyppeteer
import asyncio
from pyppeteer import launch
async def main():
browser = await launch()
page = await browser.newPage()
await page.goto('https://google.com')
if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())
Executing the above command will start Chronium in headless mode, open the Google site and close the browser. Chromium will be installed only once during the first import. By default, Pyppeteer launches the browser in headless mode.
browser = await launch(headless=False)
You can display the browser by doing.
#Get the first element that meets the criteria
# page.Also possible with J
textbox = await page.querySelector('input[aria-label="Search"]')
textbox = await page.J('input[aria-label="Search"]')
#Get all the elements that meet the conditions
# page.Also possible with JJ
buttons = await page.querySelectorAll('input[aria-label="Google search"]')
buttons = await page.JJ('input[aria-label="Google search"]')
# page.type(selector,Input value)
#Possible even from the acquired element
await page.type('input[aria-label="Search"]', 'pyppeteer')
await textbox.type('pyppeteer')
# page.click(selector)
#It is possible even from the acquired element
#Because Google's search button has to select the second element
#Should I get it with querySelectorAll?
await page.click('last-child:input[aria-label="Google search"]')
await buttons[1].click()
Get it by executing javascript with page.evaluate.
text = await page.evaluate('elm => elm.getAttribute("name")',textbox)
Same as above
elm = await page.J('#hptl')
text = await page.evaluate('elm => elm.innerHTML', elm)
Recommended Posts