亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python多線程抓取天涯帖子內(nèi)容示例

 更新時間:2014年04月03日 10:21:45   作者:  
這篇文章主要介紹了python多線程抓取天涯帖子內(nèi)容示例,需要的朋友可以參考下

使用re, urllib, threading 多線程抓取天涯帖子內(nèi)容,設(shè)置url為需抓取的天涯帖子的第一頁,設(shè)置file_name為下載后的文件名

復(fù)制代碼 代碼如下:

#coding:utf-8

import urllib
import re
import threading
import os, time

class Down_Tianya(threading.Thread):
    """多線程下載"""
    def __init__(self, url, num, dt):
        threading.Thread.__init__(self)
        self.url = url
        self.num = num
        self.txt_dict = dt

    def run(self):
        print 'downling from %s' % self.url
        self.down_text()

    def down_text(self):
        """根據(jù)傳入的url抓出各頁內(nèi)容,按頁數(shù)做鍵存入字典"""
        html_content =urllib.urlopen(self.url).read()
        text_pattern = re.compile('<span>時間:(.*?)</span>.*?<!-- <div class="host-ico">樓主</div> -->.*?<div class="bbs-content.*?>\s*(.*?)</div>', re.DOTALL)
        text = text_pattern.findall(html_content)
        text_join = ['\r\n\r\n\r\n\r\n'.join(item) for item in text]
        self.txt_dict[self.num] = text_join

 



def page(url):
    """根據(jù)第一頁地址抓取總頁數(shù)"""
    html_page = urllib.urlopen(url).read()
    page_pattern = re.compile(r'<a href="\S*?">(\d*)</a>\s*<a href="\S*?" class="\S*?">下頁</a>')
    page_result = page_pattern.search(html_page)
    if page_result:
        page_num = int(page_result.group(1))
        return page_num

 

def write_text(dict, fn):
    """把字典內(nèi)容按鍵(頁數(shù))寫入文本,每個鍵值為每頁內(nèi)容的list列表"""
    tx_file = open(fn, 'w+')
    pn = len(dict)
    for i in range(1, pn+1):
        tx_list = dict[i]
        for tx in tx_list:
            tx = tx.replace('<br>', '\r\n').replace('<br />', '\r\n').replace('&nbsp;', '')
            tx_file.write(tx.strip()+'\r\n'*4)
    tx_file.close()


def main():
    url = 'http://bbs.tianya.cn/post-16-996521-1.shtml'
    file_name ='abc.txt'
    my_page = page(url)
    my_dict = {}

    print 'page num is : %s' % my_page

    threads = []

    """根據(jù)頁數(shù)構(gòu)造urls進(jìn)行多線程下載"""
    for num in range(1, my_page+1):
        myurl = '%s%s.shtml' % (url[:-7], num)
        downlist = Down_Tianya(myurl, num, my_dict)
        downlist.start()
        threads.append(downlist)

    """檢查下載完成后再進(jìn)行寫入"""
    for t in threads:
        t.join()

    write_text(my_dict, file_name)

    print 'All download finished. Save file at directory: %s' % os.getcwd()

if __name__ == '__main__':
    main()

down_tianya.py

復(fù)制代碼 代碼如下:

#coding:utf-8

import urllib
import re
import threading
import os

class Down_Tianya(threading.Thread):
    """多線程下載"""
    def __init__(self, url, num, dt):
        threading.Thread.__init__(self)
        self.url = url
        self.num = num
        self.txt_dict = dt

    def run(self):
        print 'downling from %s' % self.url
        self.down_text()

    def down_text(self):
        """根據(jù)傳入的url抓出各頁內(nèi)容,按頁數(shù)做鍵存入字典"""
        html_content =urllib.urlopen(self.url).read()
        text_pattern = re.compile('<div class="atl-item".*?<span>時間:(.*?)</span>.*?<!-- <div class="host-ico">樓主</div> -->.*?<div class="bbs-content.*?>\s*(.*?)</div>', re.DOTALL)
        text = text_pattern.findall(html_content)
        text_join = ['\r\n\r\n\r\n\r\n'.join(item) for item in text]
        self.txt_dict[self.num] = text_join

 



def page(url):
    """根據(jù)第一頁地址抓取總頁數(shù)"""
    html_page = urllib.urlopen(url).read()
    page_pattern = re.compile(r'<a href="\S*?">(\d*)</a>\s*<a href="\S*?" class="\S*?">下頁</a>')
    page_result = page_pattern.search(html_page)
    if page_result:
        page_num = int(page_result.group(1))
        return page_num

 

def write_text(dict, fn):
    """把字典內(nèi)容按鍵(頁數(shù))寫入文本,每個鍵值為每頁內(nèi)容的list列表"""
    tx_file = open(fn, 'w+')
    pn = len(dict)
    for i in range(1, pn+1):
        tx_list = dict[i]
        for tx in tx_list:
            tx = tx.replace('<br>', '\r\n').replace('<br />', '\r\n').replace('&nbsp;', '')
            tx_file.write(tx.strip()+'\r\n'*4)
    tx_file.close()


def main():
    url = 'http://bbs.tianya.cn/post-16-996521-1.shtml'
    file_name ='abc.txt'
    my_page = page(url)
    my_dict = {}

    print 'page num is : %s' % my_page

    threads = []

    """根據(jù)頁數(shù)構(gòu)造urls進(jìn)行多線程下載"""
    for num in range(1, my_page+1):
        myurl = '%s%s.shtml' % (url[:-7], num)
        downlist = Down_Tianya(myurl, num, my_dict)
        downlist.start()
        threads.append(downlist)

    """檢查下載完成后再進(jìn)行寫入"""
    for t in threads:
        t.join()

    write_text(my_dict, file_name)

    print 'All download finished. Save file at directory: %s' % os.getcwd()

if __name__ == '__main__':
    main()

相關(guān)文章

  • 使用Python第三方庫pygame寫個貪吃蛇小游戲

    使用Python第三方庫pygame寫個貪吃蛇小游戲

    這篇文章主要介紹了使用Python第三方庫pygame寫個貪吃蛇小游戲,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • Python實現(xiàn)自定義異常堆棧信息的示例代碼

    Python實現(xiàn)自定義異常堆棧信息的示例代碼

    當(dāng)我們的程序報錯時,解釋器會將整個異常的堆棧信息全部輸出出來。解釋器會將異常產(chǎn)生的整個調(diào)用鏈都給打印出來,那么問題來了,我們能不能自定義這些報錯信息呢?本文就來為大家詳細(xì)講講
    2022-07-07
  • 詳細(xì)介紹Python進(jìn)度條tqdm的使用

    詳細(xì)介紹Python進(jìn)度條tqdm的使用

    這篇文章主要介紹了詳細(xì)介紹Python進(jìn)度條tqdm的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • keras-siamese用自己的數(shù)據(jù)集實現(xiàn)詳解

    keras-siamese用自己的數(shù)據(jù)集實現(xiàn)詳解

    這篇文章主要介紹了keras-siamese用自己的數(shù)據(jù)集實現(xiàn)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python基礎(chǔ)之輸入,輸出與高階賦值詳解

    Python基礎(chǔ)之輸入,輸出與高階賦值詳解

    這篇文章主要為大家介紹了Python基礎(chǔ)之輸入,輸出與高階賦值,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-11-11
  • Python字典簡介以及用法詳解

    Python字典簡介以及用法詳解

    字典(dictionary)是除列表意外python之中最靈活的內(nèi)置數(shù)據(jù)結(jié)構(gòu)類型。列表是有序的對象結(jié)合,字典是無序的對象集合。兩者之間的區(qū)別在于:字典當(dāng)中的元素是通過鍵來存取的,而不是通過偏移存取。
    2016-11-11
  • python重寫方法和重寫特殊構(gòu)造方法

    python重寫方法和重寫特殊構(gòu)造方法

    這篇文章主要介紹了python重寫方法和重寫特殊構(gòu)造方法,對于父類的方法,只要他不符合子類模擬的實物的行為,都可以進(jìn)行重寫,更多相關(guān)內(nèi)容需要的朋友可以參考一下
    2022-07-07
  • python -v 報錯問題的解決方法

    python -v 報錯問題的解決方法

    在本篇文章里小編給大家整理了關(guān)于python -v 報錯問題的解決方法及相關(guān)知識點,有興趣的朋友們可以學(xué)習(xí)下。
    2020-09-09
  • Python爬蟲運用正則表達(dá)式的方法和優(yōu)缺點

    Python爬蟲運用正則表達(dá)式的方法和優(yōu)缺點

    這篇文章主要給大家介紹了關(guān)于Python爬蟲運用正則表達(dá)式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • python3.7添加dlib模塊的方法

    python3.7添加dlib模塊的方法

    這篇文章主要介紹了python3.7添加dlib模塊的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07

最新評論