英文:
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())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论