运行 我网站上的 Python 脚本

Run a Python script on my website

我正在使用 beautifulsoup 从网站上抓取数据。

我的代码在 运行 来自 PyCharm 时有效。

当我 运行 它出现在我的网站上时(例如 newser.000webhost.com/new.py),它不会 运行。

如何 运行 我的代码在我的网站上?

import requests
from bs4 import BeautifulSoup

def trade_spider(max_pages):

    page = 1

    while page <= max_pages:
        url = 'https://www.geo.tv/category/sports/'+ str(page)
        source_code = requests.get(url, allow_redirects=False)
        plain_text = source_code.text.encode('ascii', 'replace')
        soup = BeautifulSoup(plain_text, 'html.parser')

        for div in soup.findAll('div', {'class': 'geo-zoom-effect'}):
            for a in div.findAll('a'):
             title = a.get('title')
             href = a.get('href')
             print title
             print href

            for img in a.findAll('img'):
                src=img.get('src')
                print src

        page+=1

trade_spider(5)

000webhost 是一个提供 HTML、CSS、PHP 和 MySQL 文件的虚拟主机,而不是 Python 主机。

如果您正在寻找完整的 UNIX 环境并托管 Python 文件,Digital Ocean 相当便宜($5/月)且可靠 VPS。

如果不熟悉或想要免费我会推荐PythonAnywhere or OpenShift