用Python构建类似Jon的声誉追踪器
Jon Skeet 有一个用 C# 做的 声望追踪器。
我想用 Python 做一个类似的应用,至少要用到以下模块:
- beautiful soup
- defaultdict
我们显然需要:
- 用 Beautiful Soup 从网站 'https://stackoverflow.com/users/#user-id#' 中解析出声望
- 用 defaultdict 来存储这些数据
那么,如何用 Python 构建一个和 Jon 的系统类似的声望系统呢?
1 个回答
4
屏幕抓取其实很简单,如果我理解了StackOverflow的HTML格式,比如说,想要获取我的声望(因为我是用户95810):
import urllib
import BeautifulSoup
page = urllib.urlopen('http://stackoverflow.com/users/95810')
soup = BeautifulSoup.BeautifulSoup(page)
therep = str(soup.find(text='Reputation').parent.previous.previous).strip()
print int(therep.replace(',',''))
不过,我不太明白你在这里想用defaultdict
干什么——你想对这个整数进行什么进一步的处理,为什么需要把它存储在defaultdict里呢?