A story about running Python on PHP on Heroku

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.

Thoughts

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.

Scraping with Python

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)

Get Python output using exec () in PHP

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.

Character code story

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)

Deploy to Heroku! Should be an error storm

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. e4d1a639c4938b5f126d5891784286ff.png 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.

Impressions of completing the race

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.

Reference material

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

A story about running Python on PHP on Heroku
A story about a 503 error on Heroku open
A memo of a tutorial on running python on heroku
A story about Python pop and append
A story about displaying article-linked ads on Jubatus
A memorandum for touching python Flask on heroku
A story about deploying a Twitter-linked app created using Flask + gunicorn on Heroku
A story about modifying Python and adding functions
A story about trying a (Golang +) Python monorepo with Bazel
A story about a Linux beginner putting Linux on a Windows tablet
About Perl, Python, PHP, Ruby
A memorandum about correlation [Python]
A memorandum about Python mock
A note about [python] __debug__
I thought about a Python beginner's course on blockchain games
A memorandum of stumbling on my personal HEROKU & Python (Flask)
A story about how to specify a relative path in python.
A story about an amateur making a breakout with python (kivy) ②
[GCP] A memorandum when running a Python program on Cloud Functions
A story about an amateur making a breakout with python (kivy) ①
A story about trying to implement a private variable in Python.
A story about a GCP beginner building a Minecraft server on GCE
A story about a python beginner stuck with No module named'http.server'
Building a Python environment on Mac
Building a Python environment on Ubuntu
Python: A Note About Classes 1 "Abstract"
Create a Python environment on Mac (2017/4)
python + django + scikit-learn + mecab (1) on heroku
python + django + scikit-learn + mecab (2) on heroku
Create a python environment on centos
Python json.loads () returns str on Heroku
A story about using Python's reduce
Build a python3 environment on CentOS7
A note about mock (Python mock library)
Periodically run Python on Heroku Scheduler
A story about adding a REST API to a daemon made with Python
Easy! Implement a Twitter bot that runs on Heroku in Python
A story about putting Chrome OS (not Chromium) on surface GO
A story about developing a soft type with Firestore + Python + OpenAPI + Typescript
A story about trying to run multiple python versions (Mac edition)
A note on using tab completion when running Python interactively on Windows
A story about wanting to think about garbled characters on GAE / P
Everything from building a Python environment to running it on Windows
A story about trying to run JavaScripthon on Windows and giving up.
Migrate Django applications running on Python 2.7 to Python 3.5
A story about remodeling Lubuntu into a Chromebook
A story about a beginner making a VTuber notification bot from scratch in Python
Build a python environment on MacOS (Catallina)
Create a python environment on your Mac
A Java programmer studied Python. (About type)
A story about machine learning with Kyasuket
A memo with Python2.7 and Python3 on CentOS
Build CGI Server running on Python 3 on Docker
Creating a python virtual environment on Windows
A story about creating an anonymous channel on Slack from zero knowledge
A story about trying to connect to MySQL using Heroku and giving up
A little more detail on python comprehensions
Run Python code on A2019 Community Edition
Build a Python + OpenCV environment on Cloud9
The story of blackjack A processing (python)
[Python] A progress bar on the terminal