专栏名称: 大数据应用
数据应用学院被评为2016北美Top Data Camp, 是最专业一站式数据科学咨询服务机构,你的数据科学求职咨询专家!
目录
相关文章推荐
艺恩数据  ·  艺恩数据祝您开工大吉! ·  1 周前  
艺恩数据  ·  春节档观众满意度亮眼 ... ·  1 周前  
艺恩数据  ·  新春贺岁,福满人间! ·  2 周前  
数据派THU  ·  Transfusion: ... ·  4 天前  
数据派THU  ·  时间序列平稳性的双重假设检验:KPSS与AD ... ·  4 天前  
51好读  ›  专栏  ›  大数据应用

学爬虫有什么用?我们一口气给你爬了知乎上161张高颜值小姐姐的照片

大数据应用  · 公众号  · 大数据  · 2019-10-16 08:27

正文

阅兵方阵中的军乐队小姐姐火了,看到朋友圈好多小伙伴说自己恋爱了。除了军乐队的小姐姐,知乎上也有很多漂亮小姐姐的照片。今天知乎上有一个热搜问题:


你见过的有些人能漂亮到什么程度?


原问题在这儿


https://www.zhihu.com/question/266808424


我们用爬虫爬了下这个问题下的高赞照片。在欣赏小姐姐的美照之前,我们先来分享一下思路。


首先通过浏览器的开发者工具,找到所有回答的链接。知乎的回答都是ajax的方式加载的,一次加载一页。


我们可以通过知乎回答的url,先把回答一页一页的爬下来,存到本地数据库。随后从数据库里读取数据,筛选出高赞的回答,把回答里的图片解析出来。


思路大致就是这样。


这里分享几个主要函数,完整代码可以在后台回复“ 知乎小姐姐 ”获取。

def get_answers_by_page(page_no):
offset = page_no * 10
url = "&offset={}&limit=10&sort_by=default&platform=desktop".format(offset)
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36",
}
r = requests.get(url, verify=False, headers=headers)
content = r.content.decode("utf-8")
data = json.loads(content)
is_end = data["paging"]["is_end"]
items = data["data"]
client = pymongo.MongoClient()
db = client["beauty"]
if len(items) > 0:
db.answers.insert_many(items)
return is_end

def get_answers():
page_no = 0
client = pymongo.MongoClient()
while True:
print(page_no)
is_end = get_answers_by_page(page_no)
page_no += 1
if is_end:
break

def query():
client = pymongo.MongoClient()
db = client["beauty"]
items = db.answers.find({"voteup_count": {"$gte": 100}}).sort([("voteup_count", pymongo.DESCENDING)])
count = 0

for item in items:
content = item["content"]
vote_num = item["voteup_count"]
author = item["author"]["name"]
matched = re.findall(r'data-original="([^"]+)"', content)
print("> 来自 {}\n".format(item["url"]))
print("> 作者 {}\n".format(author))
print("> 赞数 {}\n".format(vote_num))
img_urls = []
for img_url in matched:
if img_url not in img_urls:
print("![]({})".format(img_url))
img_urls.append(img_url)
count += len(img_urls)
print("\n\n")
print(count)


上面是3个函数。

  • get_answers_by_page - 这个函数用于获取一页的回答内容,获取的内容会存到本地MongoDB里

  • get_answers - 这个函数用于获取所有页的内容,它会调用上面的函数,循环获取每一页的内容

  • query - 这个函数用于从MongoDB里查询数据,筛选高赞回答,并且把结果打印出来


如果要完整的运行这个项目,大家可以下载源代码后,在本地运行。


运行项目后,程序会筛选出所有赞数大于100的回答,并且把回答里的图片整理出来。赞数越高的回答,小姐姐的颜值越高。


我们来欣赏一下部分小姐姐的美照


来自 https://www.zhihu.com/api/v4/answers/616931654

作者 不知

赞数 24153

来自 https://www.zhihu.com/api/v4/answers/768553689

作者 一只喝酒的猫Miao

赞数 17702

来自 https://www.zhihu.com/api/v4/answers/626983318

作者 匿名用户

赞数 17432

来自 https://www.zhihu.com/api/v4/answers/621175755

作者 知乎用户

赞数 16764

来自 https://www.zhihu.com/api/v4/answers/792418115

作者 匿名用户

赞数 16285

来自 https://www.zhihu.com/api/v4/answers/664414526

作者 万金油

赞数 13502

来自 https://www.zhihu.com/api/v4/answers/612795804

作者 樱桃玩

赞数 13320

来自 https://www.zhihu.com/api/v4/answers/612018288

作者 JoyCE

赞数 12990

来自 https://www.zhihu.com/api/v4/answers/664315159

作者 开眼视频App

赞数 11786

来自 https://www.zhihu.com/api/v4/answers/670086163

作者 William

赞数 11706







请到「今天看啥」查看全文