当它无法识别时,它应该如何用漂亮的汤 4 解析它td.font.unwrap()
?我应该更换它还是什么?或打开它?
<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()
感谢您的帮助!谢谢