【问题标题】:How to use php script in python如何在python中使用php脚本
【发布时间】:2017-06-16 12:55:36
【问题描述】:

我正在尝试为 python 3.5 中的项目编写一些代码。我有一个用 php 编写的 API 脚本。我想在我的 python 代码中使用这个脚本。 我正在寻找一种方法,但我还没有找到。

这是我的python代码:

#Loading Libraries
import urllib
import os
import time
from urllib.parse import urlparse
from urllib.parse import urljoin
from collections import Counter
import urllib.request

from bs4 import BeautifulSoup
id= 1
url='http://scitechdaily.com/new-technique-reveals-internal-characteristics-of-photonic-crystals/'

	
def GetArticles(url,id):
	file = open('C:\\Users\\Hassan Raza\\Desktop\\Mozilla tech article\\Article'+'.txt', 'w')
	req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
	
	htmlpage = urllib.request.urlopen(req)
	html = htmlpage.read().decode('utf-8')

	soup = BeautifulSoup(html,"html.parser")

	title= soup.find_all('h1', {'class','title'})
	for titles in title:
		print(titles.text)
	text = soup.find_all('div' , {'class', 'entry'})
	for pg in text:
		textdata = pg.text.encode('utf8')


		############ Here I want to pass textdata to an API which is wirtten in PHP##########################
		
		
	file.close()
	
GetArticles(url,id)

请让我知道如何在 python 中使用 php 脚本,以便我可以参与我的项目。

【问题讨论】:

    标签: php python-3.x beautifulsoup subprocess


    【解决方案1】:

    是的,这是可能的。

    $ cat test.py
    import subprocess
    
    print subprocess.Popen(['php', '-f', 'test.php'])
    $ cat test.php
    <?php
    ini_set("allow_url_fopen", 1);
    
    $url = "https://baconipsum.com/api/?type=all-meat&paras=2&start-with-lorem=1";
    
    $json = file_get_contents($url);
    $obj = json_decode($json);
    
    print_r($obj);
    $ python test.py
    <subprocess.Popen object at 0x7f968fcefd90>
    $ Array
    (
        [0] => Bacon ipsum dolor amet frankfurter beef ribs jowl alcatra kielbasa pork loin andouille.  Filet mignon pork chop bacon brisket meatball, strip steak picanha kielbasa tri-tip pork capicola boudin leberkas.  Tail spare ribs cow sirloin, pancetta ribeye capicola flank porchetta prosciutto pork pastrami.  Burgdoggen turducken meatball, hamburger salami bacon drumstick bresaola strip steak fatback boudin ham.  Filet mignon fatback bresaola landjaeger brisket, tri-tip flank pork belly sirloin venison.  Sausage biltong turducken pork chop turkey cow meatball bacon shankle drumstick.  Jerky cow shankle pork loin tongue ribeye leberkas landjaeger spare ribs prosciutto capicola alcatra tenderloin.
        [1] => Beef fatback frankfurter alcatra chuck ribeye.  Shankle corned beef kielbasa frankfurter bacon jerky, flank sirloin kevin ball tip burgdoggen hamburger pork belly tongue rump.  Short ribs pastrami hamburger, shank chuck short loin strip steak pork loin filet mignon sausage.  Leberkas pork loin jowl burgdoggen frankfurter pork belly.  Tongue fatback jowl ribeye.  Meatball bresaola spare ribs cupim rump pork belly tenderloin frankfurter shank picanha jerky bacon t-bone.  Pork belly spare ribs filet mignon, ground round meatball pancetta pastrami bacon pig ribeye ham frankfurter leberkas.
    )
    

    【讨论】:

      【解决方案2】:

      添加 PHP 脚本并使用 python 执行脚本

      import subprocess
      
      # if the script don't need output.
      subprocess.call(["php", "/path/to/your/script.php"])
      
      # if you want output
      proc = subprocess.Popen("php /path/to/your/script.php", shell=True, stdout=subprocess.PIPE)
      script_response = proc.stdout.read()
      

      【讨论】:

        猜你喜欢
        • 2010-11-06
        • 1970-01-01
        • 2019-12-20
        • 1970-01-01
        • 2014-03-18
        • 1970-01-01
        • 2015-04-03
        • 1970-01-01
        • 2014-07-09
        相关资源
        最近更新 更多