Python数据刮取

2024-09-24 22:17:41 发布

您现在位置:Python中文网/ 问答频道 /正文

Pyhton中的数据刮取。 代码运行良好,但它显示了我在下面提到的错误。 原因是什么?你知道吗

import urllib2
from bs4 import BeautifulSoup
from xlwt import workbook   

wb = Workbook()
sheet1 = wb.add_sheet('Sheet1')

soup = BeautifulSoup(urllib2.urlopen("http://en.wikipedia.org/wiki/List_of_Indian_satellites").read())

for row in soup('table', {'class': 'wikitable sortable jquery-tablesorter’})[0].tbody('tr'):
    tds = row('td')
    sheet1.write(row, 0, tds[0].string)
    sheet2.write(row, 1, tds[1].string)    

wb.save('Performance Project.xls')    

Error: File "ProcessingProject.py", line 12 SyntaxError: Non-ASCII character '\xe2' in file ProcessingProject.py on line 12

但没有声明编码;有关详细信息,请参见http://python.org/dev/peps/pep-0263/


Tags: infromorgimporthttpstringurllib2write
2条回答

对于soup中的行('table',{'class':'wikitable sortable jquery tablesorter'})[0].tbody('tr'):

在jquery tablesorter之后使用了错误的字符作为单引号分隔符

#!/usr/bin/env python
# -*- coding: utf-8 -*- 

将此添加到文件顶部

相关问题 更多 >