例子:解析优快云论坛帖子列表,以“脚本语言(Perl ,Python)版块”为例。 代码: #导入模块 import urllib2 from BeautifulSoup import BeautifulSoup #获取远程网页代码并交给BeautifulSoup解析 page = urllib2.urlopen('http://forum.youkuaiyun.com/SList/OL_Script//') soup = BeautifulSoup(page) #从解析后的网页里拿出我们想要的那一部分 #在这里我取出每一张帖子的标题和链接 for i in soup('td',style="word-break: break-all"): print i.a.string.encode('gb2312'),i.a['href'] 下面这个方法很笨,可以作为反面教材! ''' for i in soup('td',style="word-break: break-all"): a = str(i) g = BeautifulSoup(a) s = '' for k in g.a['href']: s = s+str(k) b = g.a.string.encode('gb2312') print b,s '''