Is there a way to write in a textarea on a web page but without using selenium or a webdriver?

huangapple go评论50阅读模式
英文:

Is there a way to write in a textarea on a web page but without using selenium or a webdriver?

问题

I just need a way to write to the textdoc.co textarea but without drivers
Thank for help guys!
tried with a post request:

import requests
from bs4 import BeautifulSoup
url = 'https://textdoc.co/###url doc###'
payload = {'txt-doc': 'Hello World'}
response = requests.post(url, data=payload)
if response.status_code == 200:
    soup = BeautifulSoup(response.content, 'html.parser')
    textarea = soup.find('textarea', {'id': 'txt-doc'})
    if textarea is not None:
        print('Text successfully inserted')
    else:
        print('Element with id "txt-doc" not found')
else:
    print('Error sending request')

But it just prints me error sending requests
Do you think there is another way?

英文:

I just need a way to write to the textdoc.co textarea but without drivers

Thank for help guys!

tried with a post request:

import requests
from bs4 import BeautifulSoup
url = 'https://textdoc.co/###url doc###'
payload = {'txt-doc': 'Hello World'}
response = requests.post(url, data=payload)
if response.status_code == 200:
    soup = BeautifulSoup(response.content, 'html.parser')
    textarea = soup.find('textarea', {'id': 'txt-doc'})
    if textarea is not None:
        print('Text successfully inserted')
    else:
        print('Element with id "txt-doc" not found')
else:
    print('Error sending request')

But it just prints me error sending requests

Do you think there is another way?

答案1

得分: 0

以下是您提供的代码的翻译部分:

您可以使用 [Pyppeteer](https://pyppeteer.github.io/pyppeteer/index.html) 模拟键入点击打开 URL 等操作例如

from pyppeteer import launch
import time
import asyncio
import nest_asyncio

nest_asyncio.apply()

async def main():
    browser = await launch({
        "headless": False
    })
    page = await browser.newPage()
    id_doc = '###url doc###'
    await page.goto('https://textdoc.co/'+id_doc)
    title = 'My Title'
    description = 'My Description'
    await page.evaluate('()=>{ document.querySelector("input#txt-name").value = "" }')
    await page.click('input#txt-name')
    await page.type('input#txt-name', title)
    await page.evaluate('()=>{ document.querySelector("textarea#txt-doc").value = "" }')
    await page.click('textarea#txt-doc')
    await page.type('textarea#txt-doc', description)
    time.sleep(2)  # 等待 2 秒
    await page.click('main')
    time.sleep(2)  # 等待 2 秒
    # await page.click('button[title="Download"]')
    await page.evaluate('()=>{ location.href = "https://textdoc.co/home/download/'+id_doc+'" }')
    time.sleep(10)  # 等待 10 秒
    await browser.close()
asyncio.get_event_loop().run_until_complete(main())

希望这对您有所帮助!

英文:

You can simulate typing, clicking, open URL, etc with Pyppeteer. For example:

from pyppeteer import launch
import time
import asyncio
import nest_asyncio

nest_asyncio.apply()

async def main():
    browser = await launch({
        "headless": False
    })
    page = await browser.newPage()
    id_doc = '###url doc###'
    await page.goto('https://textdoc.co/'+id_doc)
    title = 'My Title'
    description = 'My Description'
    await page.evaluate('()=>{ document.querySelector("input#txt-name").value = "" }')
    await page.click('input#txt-name')
    await page.type('input#txt-name', title)
    await page.evaluate('()=>{ document.querySelector("textarea#txt-doc").value = "" }')
    await page.click('textarea#txt-doc')
    await page.type('textarea#txt-doc', description)
    time.sleep(2)  # sleep 2 seconds
    await page.click('main')
    time.sleep(2)  # sleep 2 seconds
    # await page.click('button[title="Download"]')
    await page.evaluate('()=>{ location.href = "https://textdoc.co/home/download/'+id_doc+'"}')
    time.sleep(10)  # sleep 10 seconds
    await browser.close()
asyncio.get_event_loop().run_until_complete(main())

huangapple
  • 本文由 发表于 2023年4月17日 01:40:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029374.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定