代码更改以支持python2.x和beautifulsoup3.x

时间:2012-05-30 19:41:12

标签: python beautifulsoup

我希望改变以下代码,以便在python2.x和beautifulsoup3.x上运行它

import requests
import BeautifulSoup

session = requests.session()

pages = []

req = session.get('webpage')

content = req.content.split("</html>")


for page in content[:-1]:
    doc = BeautifulSoup.BeautifulSoup(page)

    name = doc.find('table', id='table2').find('table').findAll('td')[3].text
    print name

    tables = doc.findAll('table', id="conn")
    target_table = None
    for table in tables:
        try:
            title = table.find('thead').find('td').text
        except:
            title = None
        if title == 'ESME  DETAILS':
            target_table = table
            break
    if target_table:
        esme_trs = target_table.find('tbody').findAll('tr')
        for tr in esme_trs:
            print "\t", tr.find('td').text

2 个答案:

答案 0 :(得分:0)

设置requests时,要么将默认的Python安装设置为Py2.x,要么通过源代码安装requests而不是仅运行python setup.py install而是运行/path/to/python2.x setup.py install将它安装到2.x实例。

答案 1 :(得分:0)

问题是python2.X安装中没有安装requests,仅适用于python3.X

requests不是标准库,所以它不会安装python,所以你需要手动安装它。

请参阅requests website上有关如何安装的说明。