doukuangxun5382 2017-01-31 07:10
浏览 91

如何在python中使用php脚本

I am trying to write some code for a project in python 3.5. I have an API script which is written in php. I want to use this script in my python code. I am looking for a way but I could not find one yet.

Here is my python code:

#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)

Please let me know how to use a php script in python so I can compete my project.

</div>
  • 写回答

2条回答 默认 最新

  • dongshai8330 2017-01-31 07:17
    关注

    You add PHP script and execute the script using 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()
    
    评论

报告相同问题?