无需安装即可在CGI中使用BeautifulSoup [英] Using BeautifulSoup in CGI without installing

查看:129
本文介绍了无需安装即可在CGI中使用BeautifulSoup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Python构建一个简单的刮板,它将通过CGI在Web服务器上运行.基本上,它将返回一个值,该值由URL中传递给它的参数确定.我需要BeautifulSoup来处理Web服务器上的HTML页面.但是,我使用的是HelioHost,它不能给我提供shell访问权限或pip等功能.我只能使用FTP. BS网站之一,它说您可以直接将其提取并使用而无需安装.

所以我在Win7机器上安装了tarball,使用7-zip删除bz2压缩,然后使用tar压缩,这给了我bs4文件夹和setup.py文件.我将完整的bs4文件夹通过ftp转移到了我的cgi-bin目录中,该脚本是python脚本所在的目录.我的脚本代码是:

#!/usr/bin/python
import cgitb
cgitb.enable()


import urllib
import urllib2
from bs4 import *

print "Content-type: text/html\n\n"
print "<html><head><title>CGI Demo</title></head>"
print "<h1>Hello World</h1>"
print "</html>"

但这给我一个错误:

 /home/poiasd/public_html/cgi-bin/lel.py
    6 import urllib
    7 import urllib2
    8 from bs4 import *
    9 
   10 print "Content-type: text/html\n\n"
bs4 undefined
SyntaxError: invalid syntax (__init__.py, line 29) 
      args = ('invalid syntax', ('/home/poiasd/public_html/cgi-bin/bs4/__init__.py', 29, 6, 'from .builder import builder_registry\n')) 
      filename = '/home/poiasd/public_html/cgi-bin/bs4/__init__.py' 
      lineno = 29 
      msg = 'invalid syntax' 
      offset = 6 
      print_file_and_line = None 
      text = 'from .builder import builder_registry\n'

如何通过CGI使用bs4模块?如何安装但不安装?我可以将PC上的BeautifulSoup转换为一个漂亮的小BeautifulSoup4.py,其中包含所有代码吗?

解决方案

您正在使用的Python版本尚不支持 BeautifulSoup 3 ;它将有一些错误,并且您将缺少一些功能,但是至少您可以克服语法错误.

但是,我注意到HelioHost确实列出了受支持的Python 2.7 . /p>

I am trying to build a simple scraper in Python, which will run on a Webserver via CGI. Basically it will return a value determined by a parameter passed to it in a URL. I need BeautifulSoup to do the processing of HTML pages on the webserver. However, I'm using HelioHost, which doesn't give me shell access or pip etc. I can only use FTP. One the BS website, it says you can directly extract it and use it without installing.

So I got the tarball on my Win7 machine, used 7-zip to remove bz2 compression, and then tar compression, which gave me a bs4 folder and a setup.py file. I transferred the complete bs4 folder to my cgi-bin directory where the python script is located via ftp. My script code is :

#!/usr/bin/python
import cgitb
cgitb.enable()


import urllib
import urllib2
from bs4 import *

print "Content-type: text/html\n\n"
print "<html><head><title>CGI Demo</title></head>"
print "<h1>Hello World</h1>"
print "</html>"

But it is giving me an error:

 /home/poiasd/public_html/cgi-bin/lel.py
    6 import urllib
    7 import urllib2
    8 from bs4 import *
    9 
   10 print "Content-type: text/html\n\n"
bs4 undefined
SyntaxError: invalid syntax (__init__.py, line 29) 
      args = ('invalid syntax', ('/home/poiasd/public_html/cgi-bin/bs4/__init__.py', 29, 6, 'from .builder import builder_registry\n')) 
      filename = '/home/poiasd/public_html/cgi-bin/bs4/__init__.py' 
      lineno = 29 
      msg = 'invalid syntax' 
      offset = 6 
      print_file_and_line = None 
      text = 'from .builder import builder_registry\n'

How can I use the bs4 module via CGI? How can I install but not-install it? Can I convert the BeautifulSoup I have on my PC to a nice little BeautifulSoup4.py which will contain all the code?

解决方案

You are using a version of Python that doesn't yet support PEP 328 Relative Imports; e.g. Python 2.4 or older. BeautifulSoup 4 requires Python 2.7 or newer.

Presumably you cannot upgrade to a newer Python version. In that case you can try using BeautifulSoup 3; it'll have a few bugs and you'll be missing some features, but at least you can get past the syntax error.

However, I note that HelioHost does list Python 2.7 as supported.

这篇关于无需安装即可在CGI中使用BeautifulSoup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆