0

当它无法识别时,它应该如何用漂亮的汤 4 解析它td.font.unwrap()?我应该更换它还是什么?或打开它?

<td align="CENTER">
    <font size="+2">横&lt;/font>(F
    <font size="+2">橫&lt;/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()

感谢您的帮助!谢谢

4

1 回答 1

1

该网站声称在 gb2312 中,但事实并非如此。这应该解决它:

url = 'http://www.zein.se/patrick/3000char.html'
soup = bs4.BeautifulSoup(urllib2.urlopen(url).read(), from_encoding='gb18030')

要不就

soup = bs4.BeautifulSoup(urllib2.urlopen(url).read(), from_encoding='gbk')

您的浏览器可以理解,但 BeautifulSoup 需要提示。

于 2013-02-26T00:25:21.207 回答