(爬)泉洲便民資訊標題+生成云詞

 泉洲便民資訊標題+生成詞云


放假啦~~連放4天,改實作一下爬蟲。

今天的目標是大陸泉洲的便民網站,因為是簡體,所以要多一個步驟
顯示的文字不要使它變亂碼。







顯示有點奇怪,但沒關係,我只要它的標題就好

要用的庫

import requests
import bs4

先來解析它的網頁

import requests
import bs4

url='https://m.qzwb.com/gb/node/node_1467.htm'
htmlfile=requests.get(url)
bs=bs4.BeautifulSoup(htmlfile.text, 'lxml')
print(bs)

結果它出現亂碼


所以要把它解碼成utf-8

import requests
import bs4

url='https://m.qzwb.com/gb/node/node_1467.htm'
htmlfile=requests.get(url)
htmlfile.encoding='utf-8'
bs=bs4.BeautifulSoup(htmlfile.text, 'lxml')
print(bs)


接下來解析標題的結構


然後知道了大概標題的結構就可抓取並印出

import requests
import bs4


def get_html(url):
    htmlfile=requests.get(url)
    htmlfile.encoding='utf-8'
    bs=bs4.BeautifulSoup(htmlfile.text, 'lxml')
    ul=bs.find_all('li')
    title=''
    for i in ul:
        t2=i.find('a')
        span=i.find('div''time')
        #print(t2.text)
        if span is None:
            pass
        else:
            t1=span.text.strip()
            title+=t1
    return title,t2
url='https://m.qzwb.com/gb/node/node_1467.htm'
print(get_html(url))



云詞所須要的庫

import jieba
import numpy as np
from PIL import Image
from wordcloud import WordCloud, STOPWORDS


程式碼

'''生成詞云圖片'''
import jieba
import numpy as np
from PIL import Image
from wordcloud import WordCloud, STOPWORDS

def wd_generate(content):
    wdSpilt=' '.join(jieba.lcut(content)) #切割串列裡的字
    mask=np.array(Image.open('001.jpg')) #打開要當做詞云的背景圖
    wc=WordCloud(
        background_color='white',  # 背景颜色
        font_path=None,  # 字体
        max_words=3000,  # 最大词数
        max_font_size=100,  # 显示字体最大值
        random_state=42,  # 为每个词返回一个PIL颜色
        mask=mask,  # 以该参数值作图绘制词云
        stopwords=STOPWORDS,  # 屏蔽词
    ).generate(wdSpilt)
    wc.to_file('詞云.jpg'#生成詞云儲存自設的jpg檔
    img=Image.open('詞云.jpg'#打開完成詞云圖
    img.show() #顯示


全部程式碼如下

import requests
import bs4


def get_html(url):
    htmlfile=requests.get(url)
    htmlfile.encoding='utf-8'
    bs=bs4.BeautifulSoup(htmlfile.text, 'lxml')
    ul=bs.find_all('li')
    title=''
    for i in ul:
        t=i.find('a')
        span=i.find('div''time')
        print(t.text)
        if span is None:
            pass
        else:
            t=span.text.strip()
            title+=t
    return title

'''生成詞云圖片'''
import jieba
import numpy as np
from PIL import Image
from wordcloud import WordCloud, STOPWORDS

def wd_generate(content):
    wdSpilt=' '.join(jieba.lcut(content)) #切割串列裡的字
    mask=np.array(Image.open('001.jpg')) #打開要當做詞云的背景圖
    wc=WordCloud(
        background_color='white',  # 背景颜色
        font_path=None,  # 字体
        max_words=3000,  # 最大词数
        max_font_size=100,  # 显示字体最大值
        random_state=42,  # 为每个词返回一个PIL颜色
        mask=mask,  # 以该参数值作图绘制词云
        stopwords=STOPWORDS,  # 屏蔽词
    ).generate(wdSpilt)
    wc.to_file('詞云.jpg'#生成詞云儲存自設的jpg檔
    img=Image.open('詞云.jpg'#打開完成詞云圖
    img.show() #顯示

url='https://m.qzwb.com/gb/node/node_1467.htm'
content=get_html(url)
wd_generate(content)


留言

這個網誌中的熱門文章

(爬)微信公眾號上的圖片下載

(爬)康是美門市查詢並轉存csv