用chardect庫解決網頁亂碼問題

寫爬蟲時常常對網址發起請求,結果返回的html數據除了標籤能看懂,其餘的所有是亂碼。你們若是對爬蟲感興趣,請耐心閱讀本文,咱們就以百度風雨榜爬蟲爲例學習下亂碼處理問題。html

http://top.baidu.com/buzz?b=1
用chardect庫解決網頁亂碼問題
用chardect庫解決網頁亂碼問題
百度風雲榜一共有50個關鍵詞,咱們先任選其中一個打開看看。 正則表達式

用chardect庫解決網頁亂碼問題

1、實驗目的
咱們的目的是學會使用chardect庫,對亂碼內容進行處理。本文會以一個具體的例子,經過實戰的方式獲取正確編碼的數據。瀏覽器

2、代碼實戰
2.1 定位關鍵詞及其連接
F12鍵盤打開開發者工具,咱們定位關鍵詞及其對應的html標籤。在這裏咱們使用pyquery庫定位 class屬性爲'keyword'的td。ide

用chardect庫解決網頁亂碼問題

#百度風雲榜頁面網址(含有50個熱門新聞的關鍵詞)
fengyunbang_url = 'http://top.baidu.com/buzz?b=1'

resp = requests.get(fengyunbang_url)

#從html文件中解析出  事件字段和 網址字段
doc = PyQuery(resp.text)
for item in doc.items('.keyword'):
    keyword = item('a').text().split(' ')[0]

    keyword_link=item('a').attr.href    
    print(keyword,keyword_link)

運行,結果keyword所有爲亂碼,沒有一點中文的痕跡。 函數

用chardect庫解決網頁亂碼問題

這就是咱們今天要克服的問題-html編碼問題。工具

遇到這種問題問題,咱們可能會先在html標籤中查找charset字符集。通常charset值有utf-八、gbk、gb23十二、ascii等。學習

用chardect庫解決網頁亂碼問題

用chardect庫解決網頁亂碼問題

再次運行,漢字正常顯示。 測試

用chardect庫解決網頁亂碼問題

2.2 定位搜索頁面新聞連接
上面咱們獲取到了關鍵詞及其連接,瀏覽器點擊「46年吃3萬個漢堡」對應的連接,跳轉到 百度搜索頁,以下圖。 網站

用chardect庫解決網頁亂碼問題
咱們想獲取新聞內容,而要獲取新聞內容,咱們就要知道新聞對應的連接。首先咱們要定位,以下圖。這裏咱們使用另一種方式定位連接-正則表達式。 編碼

用chardect庫解決網頁亂碼問題

def get_keywords_news_links(keyword_link):
    """
    訪問關鍵詞百度網址,獲得相關新聞的link
    :param keyword_link:
    :return:
    """
    headers = {'User-Agent': '你的user-agent'}

    resp = requests.get(keyword_link, headers=headers)

    bsObj = BeautifulSoup(resp.text, 'html.parser')

    news_items = bsObj.find_all('div', {'class': 'result c-container '})

    news_links = []
    for item in news_items:

        links = re.findall('href="(.*?)"', str(item))

        news_links.extend(links)

    #防止連接重複
    news_links = set(news_links)
    return news_links

可是後來發現有的連接是沒法訪問的,好比運行中竟然抽取出含有http://cache.baiducontent***這種的網頁,通過測試,只要剔除掉這種連接,剩下的都爲有效連接。

2.3 獲取新聞文本內容
有了上文獲取到的連接,咱們寫個簡單的代碼獲取文本內容。因爲獲取到的網址來源成百上千的網站,若是要精確獲取新聞內容,須要對每個網站一對一的進行定位。這樣太麻煩,爲了快速方便,咱們使用正則匹配出全部的中文內容。

def get_news_content(link):
    """
    根據新聞網址,獲取新聞數據
    :return:  新聞內容
    """
    resp = requests.get(link)
    #最終只有漢字保留。
    news_text = ''.join(re.findall('[\u4e00-\u9fa5]+', resp.text))

    return news_text

可是運行過程當中,常常返回空。說明正則匹配中文時候匹配不到。極可能的緣由是頁面中沒有中文,可是咱們檢測了這些頁面都是有中文的,那麼頗有多是由於頁面亂碼,致使正則[\u4e00-\u9fa5]+沒法匹配到中文。通過檢測,真的是亂碼。解決辦法resp.encoding='編碼'。可是編碼是什麼值呢?這裏我用新的方法,chardect庫檢測二進制數據中採用的編碼方式。

def get_news_content(link):
    """
    根據新聞網址,獲取新聞數據
    :return:  新聞內容
    """
    resp = requests.get(link)

    news_text = ''.join(re.findall('[\u4e00-\u9fa5]+', 
    resp.text))

    #網頁亂碼,致使news_text爲空
    if not news_text:
        #根據二進制數據檢測html的編碼。resp.content獲取html二進制數據
        encoding = resp.content['encoding']
        chaset = chardet.detect(encoding)
        #解決編碼問題
        resp.encoding = chaset
        news_text = ''.join(re.findall('[\u4e00-\u9fa5]+', resp.text))

        return news_text    
    return news_text

2.4 編寫爬蟲主函數
編寫爬蟲主函數,將數據保存到csv中。

#主函數,訪問並保存全部的新聞數據
def FetchAndSave():
    #百度風雲榜頁面網址(含有50個熱門新聞的關鍵詞)
    fengyunbang_url = 'http://top.baidu.com/buzz?b=1'
    resp = requests.get(fengyunbang_url)
    resp.encoding='gb2312'

    #新建excel文件保存數據。
    csvf = open('data.csv', 'a+', encoding='gbk', newline='') as csvf

    writer = csv.writer(csvf)
    writer.writerow(('news_content', 'keyword'))

    #從heml文件中解析出  事件字段和 網址字段
    doc = PyQuery(resp.text)
    for item in doc.items('.keyword'):
        keyword = item('a').text().split(' ')[0]

        keyword_link = item('a').attr.href

        news_links = get_keywords_news_links(keyword_link)

        for news_link in news_links:
            try:
                content = get_news_content(news_link)
                #防止空內容寫入csv中
                if content:
                    writer.writerow((content, keyword))
            except:
                print(news_link)

#運行爬蟲
FetchAndSave()

運行爬蟲,採集了50個關鍵詞共388篇新聞內容。

用chardect庫解決網頁亂碼問題

有問題,問大鄧
項目代碼連接: https://pan.baidu.com/s/1f6tjIodW_RD1SXrdskdXdw 密碼: sqfb

相關文章
相關標籤/搜索