如何使用beautiful soup 4 </font>在<font>中获取文本

时间:2013-02-26 00:15:10

标签: python beautifulsoup

当它不能识别td.font.unwrap()时,如何用美丽的汤4解析这个? 我应该更换它还是什么?还是打开它?

<td align="CENTER">
    <font size="+2">横</font>(F
    <font size="+2">橫</font>)
</td>

我想得到横(F橫)的字符串 我现在得到的是:横(F )

我正在调用td字段就好了,但是却无法获得最后一个字符...... 这就是我现在正在调用它的方式 y = cols[1].text

cols<td>字段,这是行<tr>中的第二个字段....

以下完整代码:

    # coding: utf8
from pysqlite2 import dbapi2 as sqlite3
import urllib2
from bs4 import BeautifulSoup
from string import *


conn = sqlite3.connect(':memory:')
cursor = conn.cursor()

# # create a table
def createTable():
    cursor.execute("""CREATE TABLE characters
                      (rank INTEGER, word TEXT, definition TEXT) 
                   """)


def insertChar(rank,word,definition):
    cursor.execute("""INSERT INTO characters (rank,word,definition)
                        VALUES (?,?,?)""",(rank,word,definition))


def main():
    createTable()

    # u = unicode("辣", "utf-8")

    # insertChar(1,u,"123123123")

    # content = "\n".join(response.readlines()[1:])
    soup = BeautifulSoup(urllib2.urlopen('http://www.zein.se/patrick/3000char.html').read())

    # print (html_doc.prettify())   

    tables = soup.blockquote.table

    # print tables



    rows = tables.find_all('tr')[1:]
    result=[]
    for tr in rows:
        # print tr
        cols = tr.find_all('td')
        character = []
        # col = cols.fonts.unwrap()
        # x = int (cols[0].string)
        x = 0
        y = cols[1].text
        # chars = y.find_all('font') 

        z = "11"
        print y
        # y = cols[1].string
        # z = cols[2].string

        # xx = unicode(x, "utf-8")
        # yy = unicode(y , "utf-8")
        # zz = unicode(z , "utf-8")
        insertChar(x,y,z)

    conn.commit() 

main()

感谢您的帮助!感谢

0 个答案:

没有答案