• 爬虫大作业之爬取笔趣阁小说


    # _*_ coding:utf-8 _*_
    import requests
    import threading
    from bs4 import BeautifulSoup
    import re
    import os
    import time
    
    req_header={
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'max-age=0',
    'Connection': 'keep-alive',
    'Cookie': 'UM_distinctid=162f5f44f0113-0313b684ffd29e-5e4c2719-100200-162f5f44f05116; __jsluid=d26199ff490223142ead3dca3b417f0d; PHPSESSID=rcqhhvqkvd9ggs9mqarotu54n7; CNZZDATA1272873895=1502087634-1524539933-null%7C1524545333',
    'Host': 'm.biquge.com.tw',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.10 Mobile Safari/537.36',
    
    }
    req_url_base='http://m.biquge.com.tw/'           #小说主地址
    req_url=req_url_base+"wapbook/2016.html"                       #单独一本小说地址
    txt_section=req_url_base+"wapbook/"+'2016_1196115.html'                       #某一章页面地址
    
    # 请求当前章节页面  params为请求参数
    r=requests.get(str(txt_section),params=req_header)
    # #soup转换
    soup=BeautifulSoup(r.text,"html.parser")
    # #获取章节名称
    # print(soup)
    section_name=soup.select('#novelbody .content_title  h1')[0].text
    # print(section_name)
    # #获取章节文本
    section_text=soup.select('#novelbody .content_novel #novelcontent p' )[0].text
    
    # for ss in section_text.select("script"):                #删除无用项
    #     ss.decompose()
    # #按照指定格式替换章节内容,运用正则表达式
    section_text=re.sub( 's+', '
    	', section_text).strip('
    ')
    # print(section_text)
    # print('章节名:'+section_name)
    # print("章节内容:
    "+section_text)
    
    from wordcloud import WordCloud
    fo = open('1.txt', "ab+")         #打开小说文件
    # 以二进制写入章节题目 需要转换为utf-8编码,否则会出现乱码
    fo.write(('
    ' + section_name + '
    ').encode('UTF-8'))
    
    # 以二进制写入章节内容
    fo.write((section_text).encode('UTF-8'))
    fo.close()        #关闭小说文件
    
    f = open('1.txt','r',encoding="UTF-8").read()
    
    wordcloud = WordCloud(font_path='./fonts/simhei.ttf',background_color="white",width=1000, height=860, margin=2).generate(f)
    
    # width,height,margin可以设置图片属性
    
    # generate 可以对全部文本进行自动分词,但是他对中文支持不好,对中文的分词处理请看我的下一篇文章
    #wordcloud = WordCloud(font_path = r'D:Fontssimkai.ttf').generate(f)
    # 你可以通过font_path参数来设置字体集
    
    #background_color参数为设置背景颜色,默认颜色为黑色
    
    import matplotlib.pyplot as plt
    plt.imshow(wordcloud)
    plt.axis("off")
    plt.show()
    
    wordcloud.to_file('test.png')
    # 保存图片,但是在第三模块的例子中 图片大小将会按照 mask 保存
    

     

    这次的作业还是有难度的,我花了挺多时间的,主要问题就是那个我最后生成的词云没有中文,只是边框,最后是通过网上查资料找出了问题所在——wordcloud不支持中文,最后添加了中文字体的路径,就成功了!还要继续努力哟!

  • 相关阅读:
    win10系统全角半角切换
    出差(3~十六)
    问题在于读书不多而想的太多
    出差(3~十五)
    感恩
    hibernate--联合主键--XML
    hibernate--ID生成策略--XML
    hibernate--ID生成策略--annotation
    Hibernate---基础配置
    Hibernate---基础配置之日志信息slf 及搭建日志环境
  • 原文地址:https://www.cnblogs.com/bjdx1314/p/8934031.html
Copyright © 2020-2023  润新知