You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何通过PHP获取Python列表并在网站中展示?

How to Fetch a Python-Generated List in PHP for Your Website

Hey Lukas, let's work through this problem step by step. It sounds like you’ve got a Python script scraping headlines into a list, and you want to pull that data into PHP to display on your site—this is a super common use case, so let’s break down where things might be going wrong and fix them.

Common Pitfalls & Fixes

1. Your Python Script is Outputting a Raw List (Not a Parseable Format)

When you just print(newsHeadlines) in Python, you’re outputting a Python-specific list syntax (like ['Headline 1', 'Headline 2']), which PHP can’t easily parse. Instead, you need to output JSON—the universal data format for cross-language communication.

Update your Python script to use json.dumps():

import json

# Assume this is your scraped headlines list
newsHeadlines = ["Local City Council Approves New Park", "Tech Giant Unveils Latest Smartphone", "Weather Alert: Weekend Storm Expected"]

# Output the list as JSON
print(json.dumps(newsHeadlines))

2. PHP Isn’t Properly Executing the Python Script or Parsing the Output

If you’re calling the Python script from PHP but not getting the data you expect, double-check how you’re capturing and parsing the output. Here’s a reliable implementation:

// Call your Python script (use the FULL absolute path to avoid issues)
$pythonOutput = shell_exec('python3 /home/yourusername/scripts/your_scraper.py');

// Parse the JSON output into a PHP array
$newsHeadlines = json_decode($pythonOutput, true);

// Debug if parsing fails
if ($newsHeadlines === null) {
    echo "<h4>Debug Info:</h4>";
    echo "<p>JSON Parse Error: " . json_last_error_msg() . "</p>";
    echo "<p>Raw Python Output:</p><pre>" . htmlspecialchars($pythonOutput) . "</pre>";
} else {
    // Display the first 3 headlines (adjust as needed)
    echo "<h3>Latest News</h3>";
    echo "<ul>";
    foreach (array_slice($newsHeadlines, 0, 3) as $headline) {
        // Use htmlspecialchars to prevent XSS issues
        echo "<li>" . htmlspecialchars($headline) . "</li>";
    }
    echo "</ul>";
}

3. Permissions or Path Issues Are Blocking Access

This is one of the most common hidden problems:

  • Make sure your Python script has executable permissions: Run chmod +x /path/to/your_scraper.py on your server.
  • Ensure the user running PHP (usually www-data on Apache/Nginx) has read access to the script and its parent directory.
  • Always use the absolute path to your Python script in the PHP code—relative paths can fail because PHP’s working directory might not be what you expect.

4. Test Manually First

Before debugging PHP, run your Python script directly in the server’s terminal:

python3 /path/to/your_scraper.py

If this doesn’t output valid JSON, fix the Python script first. If it does, then the issue is with how PHP is calling or parsing the output.

内容的提问来源于stack exchange,提问作者Lukas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:06:48