dtyqeoc70733 2013-07-13 22:59
浏览 53
已采纳

将抓取的项目作为json传递给PHP脚本

So I want to pass my scraped items to a php script, I have in my pipeline:

class TalkPhpPipeline(object):

    def process_item(self, item, spider):
        toPHP = json.dumps(dict(item))

        os.system( '/usr/bin/php script.php %s' % toPHP)
        return item

and in my script.php

<? require_once('wp-config.php');
$string = $argv[1];
echo 'PHP see this ';
var_dump($string);

The spider talks to script.php just fine, however my script.php only sees a srting of lenght 7 like this {title:, if I instead replace toPHP with item['title'], then the php side sees [usomewhere only we know], what I really want to pass to php is a string like: {'title': [u'somewhere only we know']} or just {'title': ['somewhere only we know']}, of course with all other fields that my item contains, how do I do that?

Thanks,

  • 写回答

1条回答 默认 最新

  • dongshou9878 2013-07-13 23:47
    关注

    The shell is messing with the quotes in the command line. So use a different function (such as subprocess.call) that allows you to specify individual arguments:

    subprocess.call(['/usr/bin/php', 'script.php', json.dumps(dict(item))])
    

    Note that unless the JSON is very short, you may be better off just passing it to PHP in a different way, such as through a pipe, because operating systems have command line length limits.

    In Python, you could use the subprocess.Popen class to pipe the JSON to the PHP script, which could retrieve the JSON using fgets(STDIN).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog