【问题标题】:How to get and send data from python selenium to php如何从 python selenium 获取数据并将其发送到 php
【发布时间】:2020-09-16 18:22:07
【问题描述】:

请原谅我的英语。 我的系统是用php设计的,是关于连接亚马逊自动订购系统(由python和selenium设计的)

这是场景: 客户在我的网站上订购产品 -> 将数据发送到亚马逊自动订购系统(由 python 和 selenium 设计) -> 使用自动化系统下订单 -> 将数据发送到我的系统(php)

我想知道如何将数据发送到 python 并从 python 获取数据 我认为将 Json 格式的数据发送到 python 并从 python 获取 Json 格式的数据会起作用

这里是 python 代码 python code

这里是向 python 发送数据的临时 php 代码

function curlPostForOrder($url, $params){
    $ch = curl_init();
        
    curl_setopt($ch, CURLOPT_URL , $url);
    
    curl_setopt($ch, CURLOPT_USERPWD, $params['client_token'].":".""); 
        
    $JsonData = json_encode($params['data']);               //Json data post
    curl_setopt($ch, CURLOPT_POSTFIELDS, $JsonData);        //Set Post Data ;
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);                //Set ReturnTransfer; 
    curl_setopt($ch, CURLOPT_POST, 1);                          //Set Post value true ( regular HTTP POST )
        
    $headers = array();
    $headers[] = "Content-Type: application/x-www-form-urlencoded";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        
    $retValue['status'] =true;
    
    $ret= curl_exec($ch);
    error_log($ret);
    if ($ret==false) {
        echo 'Error: Curl Error';
        $retValue['status'] = false;
        $retValue['error_msg'] = curl_error($ch);
    }else{
        $retValue['data'] = json_decode($ret,true);
    }
    curl_close($ch);
        
    return $retValue;

这是从 python 获取数据的临时 php 代码

function curlPostForRetrieve($url, $params){
    $ch = curl_init();
        
    curl_setopt($ch, CURLOPT_URL , $url);
    curl_setopt($ch, CURLOPT_USERPWD, $params['client_token'].":".""); //Set UserPWD;
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);                //Set ReturnTransfer; 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    
    $retValue['status'] =true;
        
    $ret=curl_exec($ch);
    
    if ($ret==false) {
        echo 'Error: Curl Error';
        $retValue['status'] = false;
        $retValue['error_msg'] = curl_error($ch);
    }else{
        $retValue['data'] = json_decode($ret,true);
    }
    curl_close ($ch);
        
    return $retValue;

感谢您的意见

【问题讨论】:

  • 我想我回答了你最后一个类似的问题,但你到底在寻找什么?只是用于读取生成的 JSON 文件的 python 代码,还是更具体的代码?
  • 谢谢你的回答我看到你的回答但我还是不明白具体要做什么我想知道python代码来读取php的结果并将数据发送到php更详细

标签: python php selenium


【解决方案1】:

在 python 中,您可以编写一个简单的程序来读取 JSON 文件,然后将结果写出。这是执行此操作的两个函数:

import json

def read_json(file_path:str) -> dict:
    """Takes in a json file path and returns it's contents"""
    with open(file_path, "r") as json_file:
        content = json.load(json_file)
    return content

def store_json(data:dict, file_path:str):
    """Takes in a python dict and stores it as a .json file"""
    with open(file_path, "w") as json_file:
        json.dump(data, json_file)

read_json() 采用文件路径并返回带有数据的 python dict。然后,您可以进行任何必要的预处理并将其输入自动化脚本 (amz.py)。如果您需要存储结果,请从amz.py 获取结果并使用store_json() 将结果存储到 JSON 文件,该文件将数据作为第一个参数和文件路径,并将数据写入文件路径。

【讨论】:

  • 感谢您回答另一个问题,python代码是否可以从php中读取json格式的结果?我认为在订购时保存 Json 文件,那么这不是有效的方法
  • @kenlee 我不知道 php 是否有能力生成子进程或子进程(这可能对你有用 stackoverflow.com/questions/34892493/… )但如果有,你可以将数据作为字符串传递到python,然后使用sys.argvjson.loads()读取它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-22
  • 2021-09-18
  • 2020-08-19
  • 2018-01-14
相关资源
最近更新 更多