使用 Python Post 信息刷新网站或使用 Python 在现有选项卡中打开其他 link
Refresh Website with Python Post Information OR open other link in existing tab with Python
我现在有点迷茫,希望你能帮上忙!
我尝试在 Python 中识别使用 OpenCV 的人。到目前为止,这是有效的。通过识别出的人的id,我想显示属于她或他的信息。
为此我尝试了这样的事情:
1)
import webbrowser
webbrowser.open('http://mysite/index.php?userID=XYZ', new = 0)
但是尽管我写了 "new = 0" 每次其他用户 ID 出现在 link 时它都会打开一个新的浏览器选项卡。
2) 另一种方法是通过 http post 请求向网站发送信息,例如:
url = 'http://mysite/index.php'
query = {'personID': XYZ}
res = requests.post(url, data=query)
但是这样做我不知道如何在我的 PHP 代码中使用这个 post 命令,所以它使用属于用户 XYZ 的数据刷新站点..
你能帮帮我吗?
亲切的问候
编辑 1 - 开始:
一个使用 GET 参数的小 php 例子是这样的
<?php
echo("<html><head><title></title></head><body>");
if ($_GET['userID'] == 1) {
echo("<div id='userContent'>Hello User 1</div>");
}
else if ($_GET['userID'] == 2) {
echo("<div id='userContent'>Hello User 2</div>");
}
else {
echo("<div id='userContent'>Public, non user specific information</div>");
}
echo("</body></html>");
?>
但目前我不知道如何将此网站设为动态网站,当 Python 发送请求时显示不同的信息..
编辑 1 - 结束
目前在 webdriver 中这是不可能的。 0 标志是指 window,而不是选项卡。在 reddit here 上查看对此的讨论。另一个问题也在 Stack Overflow 上提出了这个问题,但没有得到答案。
然而,使用功能更丰富的包 Selenium 是可能的。
import time
from selenium import webdriver
link1="http://mysite/index.php?userID=ABC"
link2="http://mysite/index.php?userID=XYZ"
driver=webdriver.Firefox()
driver.get(link1)
time.sleep(5)
driver.get(link2)
Selenium 确实需要一些安装步骤:http://selenium-python.readthedocs.io/installation.html
关于使用请求,您没有理由不能执行 GET 请求,因此您不必担心处理问题post 请求:
import requests
url = "http://mysite/index.php?userID=ABC"
res = requests.get(url)
print(res.text)
没有看到更多 php 代码或了解更多详细信息,很难说哪种方法最适合您,但这应该可以解决您提到的问题。
我现在有点迷茫,希望你能帮上忙!
我尝试在 Python 中识别使用 OpenCV 的人。到目前为止,这是有效的。通过识别出的人的id,我想显示属于她或他的信息。
为此我尝试了这样的事情:
1)
import webbrowser
webbrowser.open('http://mysite/index.php?userID=XYZ', new = 0)
但是尽管我写了 "new = 0" 每次其他用户 ID 出现在 link 时它都会打开一个新的浏览器选项卡。
2) 另一种方法是通过 http post 请求向网站发送信息,例如:
url = 'http://mysite/index.php'
query = {'personID': XYZ}
res = requests.post(url, data=query)
但是这样做我不知道如何在我的 PHP 代码中使用这个 post 命令,所以它使用属于用户 XYZ 的数据刷新站点..
你能帮帮我吗?
亲切的问候
编辑 1 - 开始: 一个使用 GET 参数的小 php 例子是这样的
<?php
echo("<html><head><title></title></head><body>");
if ($_GET['userID'] == 1) {
echo("<div id='userContent'>Hello User 1</div>");
}
else if ($_GET['userID'] == 2) {
echo("<div id='userContent'>Hello User 2</div>");
}
else {
echo("<div id='userContent'>Public, non user specific information</div>");
}
echo("</body></html>");
?>
但目前我不知道如何将此网站设为动态网站,当 Python 发送请求时显示不同的信息..
编辑 1 - 结束
目前在 webdriver 中这是不可能的。 0 标志是指 window,而不是选项卡。在 reddit here 上查看对此的讨论。另一个问题也在 Stack Overflow 上提出了这个问题,但没有得到答案。
然而,使用功能更丰富的包 Selenium 是可能的。
import time
from selenium import webdriver
link1="http://mysite/index.php?userID=ABC"
link2="http://mysite/index.php?userID=XYZ"
driver=webdriver.Firefox()
driver.get(link1)
time.sleep(5)
driver.get(link2)
Selenium 确实需要一些安装步骤:http://selenium-python.readthedocs.io/installation.html
关于使用请求,您没有理由不能执行 GET 请求,因此您不必担心处理问题post 请求:
import requests
url = "http://mysite/index.php?userID=ABC"
res = requests.get(url)
print(res.text)
没有看到更多 php 代码或了解更多详细信息,很难说哪种方法最适合您,但这应该可以解决您提到的问题。