美文网首页
Python学习日记13|利用python制作简书首页热门文章关

Python学习日记13|利用python制作简书首页热门文章关

作者: 是蓝先生 | 来源:发表于2016-08-07 23:28 被阅读346次

今天是6.16号。

昨天去面越秀金融风险控制部计算机实习生,去面了才知道主要也就是做数据抓取这一块。面试过程中有问到分词,然后自己心虚的说了有接触过分词这一块,面试结果就不去想了,过不过都其实不重要了。此外还有问到:你觉得实习和项目的经历最大的收获是什么?你想从这份实习中收获什么?你觉得自己最大的优势和缺点是什么?这些问题在9月份的求职生活中应该还会遇到的。

趁现在有时间,然后看知乎又看到了图片云的一篇文章,其中还涉及到了结巴分词的使用,所以决定这两天好好研究一下怎么使用。然后找一个问题来分析,并得到最终的结果,本来想找学校BBS里面的帖子主体进行分析,但打开简书来写时,想想用简书首页的热门文章来分析也不错。

代码是6.16号运行的,但简书首页每天都在更新,所以最终得到的云标签会与其他时间运行得到的结果有所差异,但主要的核心还是掌握在实现过程中会涉及到的知识点:
1.异步加载;(往下拉时需要点击显示更多才会出现新的文章)
2.分词;
3.标签云。


一、异步加载
一般异步加载出来的页面都可以在chrome浏览器下的network中找到规律,简书中点击“点击查看更多后”可以在network中看到一个xhr文件,然后可以从headers里面直接看到新加载出来的页面的网址。

1.png

二、分词
三、标签云

最后贴上代码:


第一部分:抓取首页上热门标签下所有文章的标题
自己试了一下,简书首页热门标签文章点击查看更多最多也只能加载10页。
#!/usr/bin/env python
# * coding: utf-8 *

import requests
from bs4 import BeautifulSoup
import re
import time
from multiprocessing import Pool

start_url='http://www.jianshu.com/'
host='http://www.jianshu.com'
headers={
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Encoding':'gzip, deflate, sdch',
    'Accept-Language':'zh-CN,zh;q=0.8',
    'Cache-Control':'max-age=0',
    'Connection':'keep-alive',
    'Cookie':'read_mode=day; default_font=font2; __utmt=1; Hm_lvt_0c0e9d9b1e7d617b3e6842e85b9fb068=1466054262,1466057911,1466057983,1466064672; Hm_lpvt_0c0e9d9b1e7d617b3e6842e85b9fb068=1466065361; __utma=194070582.1669649852.1452758384.1466054263.1466064585.135; __utmb=194070582.12.10.1466064585; __utmc=194070582; __utmz=194070582.1466054263.134.11.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; __utmv=194070582.|2=User%20Type=Member=1; _session_id=amhxY2NtUENjQXdkMS9JRStlc0ZvV0RaaU96TXZrZ3pkK0Ewc280cmtQcHdTTFNOWk1PcGRkVDc4YzVDOGJhT2xXMlFkakN4WHpBT1d4a3Z1bUJUbXc9PS0tTjg2UGQ0aWZjdTFsNmlCdmhYRThPdz09--363edd825b2b9f3fd9d52848169fe2829b4ad3a3',
    # 'Host':'www.jianshu.com
    'If-None-Match':'W/"ab6ec19c0f005cad8d2c630dffbeb0a5"',
    'Referer':'http://www.jianshu.com/',
    'Upgrade-Insecure-Requests':'1',
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36'
}

links=[start_url]
i=0
def get_all_links(url):
    web_data=requests.get(url,headers=headers)
    soup=BeautifulSoup(web_data.text,'lxml')
    if soup.find(attrs={'class':'load-more'}):
        link=str(soup.find(attrs={'class':'load-more'}))
        link1=host+re.findall(r'data-url="(.*?)"',link,re.M)[0]
        links.append(link1)
        time.sleep(1)
        if len(links)<=200:  #modify the number of page you want to scrapy
            global i
            i=i+1
            get_all_links(links[i])
    else:
        pass

def get_contents(url):
    web_data=requests.get(url,headers=headers)
    soup=BeautifulSoup(web_data.text,'lxml')
    titles=soup.select('h4.title a')
    authors=soup.select('a.author-name.blue-link')
    route1=r'C:\Users\guohuaiqi\Desktop\title.txt'
    route2=r'C:\Users\guohuaiqi\Desktop\author.txt'
    with open(route1,'a') as f:
        for i in titles:
            try:
                f.write(i.get_text())
            except UnicodeEncodeError:
                continue
    with open(route2,'a') as f1:
        for j in authors:
             try:
                f1.write(j.get_text()+'\n')
             except UnicodeEncodeError:
                continue

if __name__=='__main__':
    get_all_links(start_url)
    pool=Pool()
    pool.map(get_contents,links)
    print('All have done successfully!')

第二部分:对抓取下来的标题进行分词
好几个月了还没有写完,对分词模块还是有点不会用,后面有机会再去学习。

未完待续。。。

相关文章

网友评论

      本文标题:Python学习日记13|利用python制作简书首页热门文章关

      本文链接:https://www.haomeiwen.com/subject/khymdttx.html