This is Qiita's first post. This time, I would like to leave a memorandum of how to "run Python on PHP on Heroku", which I stumbled upon for about 3 hours at a bad place. I am writing with brain death. It would be great if you could see the typographical errors with a gentle eye.
At first, I was wondering if I could post scraping information on a web page. But scraping is easy if you write it in Python! It was the entrance to the Gates of Hell that started.
I thought that I should get the top news for the time being, so I wrote the following code.
# coding: utf-8
import requests
from bs4 import BeautifulSoup
#Get the current top news page ID
toppage_url = "https://news.yahoo.co.jp/"
html = requests.get(toppage_url)
soup = BeautifulSoup(html.content, "html.parser")
topic_element = soup.select_one("li.topicsListItem")
news_link = topic_element.find("a").get("href")
news_id = int(str(news_link).replace("https://news.yahoo.co.jp/pickup/",""))
#Get news topic information
news_url = f"https://news.yahoo.co.jp/pickup/{news_id}"
html = requests.get(news_url)
soup = BeautifulSoup(html.content, "html.parser")
news_element = soup.select_one("p.pickupMain_articleSummary")
print(news_element.text)
You can execute command line commands with exec and put the output in variables. Write PHP as follows.
<?php header("Content-type: text/html; charset=utf-8"); ?>
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>Python-PHP</title>
</head>
<body>
<?php
exec("export LANG=ja_JP.UTF-8");
exec('python news.py', $output);
echo '<p>',$output[0],'</p>';
return false;
?>
</body>
</html>
The important thing is the ** exec ("export LANG = ja_JP.UTF-8"); ** part.
UTF-8, which has been coming out from earlier. This is the first of the difficult battles. PHP basically says that the character code should be written in ** UTF-8 **. Python output also needs to be unified with utf-8, which was stuck there at the beginning. (The Python side was Shift-JIS)
It's done! Deploy and see what happens! When I deploy and open the URL ...
\\ Currently Heroku can't process requests //
Yes. Here is the error. The deployment was successful, but it was in a nande state. The bottom line is that if you want to run both PHP and Python on Heroku at the same time, you'll need two BuildPacks. Let's set it like this. This time, ** Web pages are the main **, so the order is PHP-> Python.
This should be fine! Here and there! And when I try to deploy
\\ Deploy failed //
Hmmm this. When I caught the log this time, I found the following contents.
-----> App not compatible with buildpack: https://buildpack-registry.s3.amazonaws.com/buildpacks/heroku/python.tgz
More info: https://devcenter.heroku.com/articles/buildpacks#detection-failure
! Push failed
A search for this reveals that the files needed for the Python application are missing. Let's create it. Here's what you need for this Python code:
#requirement.txt
beautifulsoup4==4.9.1
requests==2.24.0
#runtime.txt
python-3.7.7
Deploying with this worked fine.
I spent a day struggling with character encoding and building an environment for Heroku. I hate Japanese. By the way, when I created a test environment with XAMPP, I was more worried about the character code than Heroku. This time, I'm happy because the display was successful for the time being.
Sites informed that BuildPack is required https://stackoverflow.com/questions/12126439/run-python-and-php-in-a-single-heroku-app-procfile
A site that knows the files necessary to prepare the execution environment of Python on Heroku https://teratail.com/questions/258801
(Site that helped deal with secret errors) After deployment code = H14 desc = "No web processes running" https://qiita.com/rebi/items/efd1c36f0a9e46222d80
Recommended Posts