使用 Mechanize 从网站获取中文字符没有返回任何内容
Using Mechanize to get Chinese characters from a website is returning nothing
我目前正在尝试抓取一个汉字以及非标准字母。在结果中,Mechanize 只是跳过了汉字或非标准字母。
我的代码:
import mechanize
import re
br = mechanize.Browser()
br.addheaders = [('User-agent', 'Mozilla/5.0')]
br.set_handle_robots(False)
html = br.open('http://hanzidb.org/character-list/by-frequency')
html = html.read().lower()
html = unicode(html, errors='ignore')
#Only get the data between <td>...</dr>
pattern2 = re.compile(r'<td>(.*?)</td>', re.MULTILINE)
match_description2 = re.findall(pattern2, html)
data = []
#Collect the content of the table
for desc in match_description2:
data.append(desc)
print desc
我应该得到的结果(示例):
<tr><td><a href="/character/是">是</a></td><td><span style="color:#000099;">shì</span></td><td><span class="smmr">indeed, yes, right; to be; demonstrative pronoun, this, that</span></td><td><a href="/character/日" title="Kangxi radical 72">日</a> 72.5</td><td>9</td><td>1</td><td>1479</td></td><td>3</td></tr>
与我得到的结果相比:
<td><a href="/character/"></a></td><td><span style="color:#000099;">sh</span></td><td><span class="smmr">indeed, yes, right; to be; demonstrative pronoun, this, that</span></td><td><a href="/character/" title="kangxi radical 72"></a> 72.5</td><td>9</td><td>1</td><td>1479</td></td><td>3</td>
感谢任何帮助,如果需要,我可以提供更多信息。
您必须删除行 html = unicode(html, errors='ignore')
你的LANG终端环境必须是UTF-8
和运行你的代码!
我目前正在尝试抓取一个汉字以及非标准字母。在结果中,Mechanize 只是跳过了汉字或非标准字母。
我的代码:
import mechanize
import re
br = mechanize.Browser()
br.addheaders = [('User-agent', 'Mozilla/5.0')]
br.set_handle_robots(False)
html = br.open('http://hanzidb.org/character-list/by-frequency')
html = html.read().lower()
html = unicode(html, errors='ignore')
#Only get the data between <td>...</dr>
pattern2 = re.compile(r'<td>(.*?)</td>', re.MULTILINE)
match_description2 = re.findall(pattern2, html)
data = []
#Collect the content of the table
for desc in match_description2:
data.append(desc)
print desc
我应该得到的结果(示例):
<tr><td><a href="/character/是">是</a></td><td><span style="color:#000099;">shì</span></td><td><span class="smmr">indeed, yes, right; to be; demonstrative pronoun, this, that</span></td><td><a href="/character/日" title="Kangxi radical 72">日</a> 72.5</td><td>9</td><td>1</td><td>1479</td></td><td>3</td></tr>
与我得到的结果相比:
<td><a href="/character/"></a></td><td><span style="color:#000099;">sh</span></td><td><span class="smmr">indeed, yes, right; to be; demonstrative pronoun, this, that</span></td><td><a href="/character/" title="kangxi radical 72"></a> 72.5</td><td>9</td><td>1</td><td>1479</td></td><td>3</td>
感谢任何帮助,如果需要,我可以提供更多信息。
您必须删除行 html = unicode(html, errors='ignore')
你的LANG终端环境必须是UTF-8
和运行你的代码!