我将如何在python中创建一个将文本输入到网页中的机器人 [英] How would I be able to create a bot in python that inputs text into a webpage

查看:68
本文介绍了我将如何在python中创建一个将文本输入到网页中的机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用什么代码将文本输入到框中(而不是编辑文本,类似消息漫游器的内容)?

What code would I use to input text into a box (not edit text, something along the lines of a message bot)?

说我运行代码,它会打开一个网页并将文本输入到(例如)搜索框中.

Say I run the code, it opens a webpage and inputs text into a (e.g.) search box.

谢谢!

推荐答案

您可以使用库,用Python创建一个将文本输入到网页中的机器人.

You can use the Selenium library to create a bot in Python that inputs texts into a webpage.

根据它们的描述,使用硒包自动实现来自Python的Web浏览器交互."

您可以通过在终端中输入以下内容来安装它:

You can install it by typing in the terminal:

pip安装硒

然后,您必须构建一组指令来告诉程序您想要做什么.

Then you'll have to build a set of instructions to tell the program what you want to do.

示例:

任务:

1. open a new Firefox browser
2. load the Yahoo homepage
3. search for "seleniumhq"
4. close the browser

代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox()

browser.get('http://www.yahoo.com')
assert 'Yahoo' in browser.title

elem = browser.find_element_by_name('p')  # Find the search box
elem.send_keys('seleniumhq' + Keys.RETURN)

browser.quit()

此外,请阅读 Selenium Docs 并观看一些视频,以了解如何构建自己的网络机器人!

Also, read the Selenium Docs and take a look at a few videos to understand how can you build your own web bot!

这篇关于我将如何在python中创建一个将文本输入到网页中的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆