PHPにおいてPythonのprint内容を取得しようとしています。
Python
py
1# news.py 2import random 3import requests 4from bs4 import BeautifulSoup 5 6# 現在のトップニュースのページIDを取得する 7toppage_url = "https://news.yahoo.co.jp/" 8html = requests.get(toppage_url) 9soup = BeautifulSoup(html.content, "html.parser") 10topic_element = soup.select_one("li.topicsListItem") 11news_link = topic_element.find("a").get("href") 12news_id = int(str(news_link).replace("https://news.yahoo.co.jp/pickup/","")) 13 14# ニュースのトピック情報を取得する 15news_url = f"https://news.yahoo.co.jp/pickup/{random.randint(news_id-100,news_id)}" 16html = requests.get(news_url) 17soup = BeautifulSoup(html.content, "html.parser") 18news_element = soup.select_one("p.pickupMain_articleSummary") 19print(news_element.text)
PHP
php
1<!doctype html> 2<html lang="ja"> 3<head> 4 <meta charset="utf-8"> 5 <title>テスト</title> 6</head> 7<body> 8 9<?php 10exec('python news.py', $output); 11echo '<p>',$output[0],'</p>'; 12?> 13 14</body> 15</html>
このようにすると文字化けが起きます。どちらもファイル自体の文字コードはutf-8で統一しています。
https://miyabikno-jobs.com/embed-php-python2/
ここを参考し、exec('export LANG=ja_JP.UTF-8; python news.py', $output);を実行すると、
Notice: Undefined offset: 0 in \test\index.php on line 11
とでて、それ以上何も出てこないです。
Pythonの出力結果は日本語です。対処方法のほどご指導お願いします。
あなたの回答
tips
プレビュー