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

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度