使用python 多線程自動採集內容併發布在自己的網站

這是自動採集系列的第三步,達到了預先的設想

 

#coded by 伊瑪目的門徒
#coding=utf-8
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts, NewPost
from wordpress_xmlrpc.methods.users import GetUserInfo
import time
import requests
from bs4 import BeautifulSoup
import re


from concurrent.futures import ThreadPoolExecutor


start = time.clock()  # 計時-開始
urllist=[]
titlelist=[]

header={'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.XXXX.XXX Safari/537.36'}



def do(i):
    try:

        cd=[]
        html=requests.get('http://futures.hexun.com/domestic/index-'+str(i)+'.html',headers=header)

        html.encoding='gbk'

        Soup = BeautifulSoup(html.text, "lxml")
        #ab=Soup.select('li a[target="_blank"]')
        ab=Soup.select('div.temp01 ul li a[target="_blank"]')

        for x in range(len(ab)):
            if (x % 2) == 1:
                cd.append (ab[x])

        #print ('--------------')

        pattern = re.compile(r'<a href="(.*?)" target="_blank">',re.S)   # 查找數字
        result1 = pattern.findall(str(cd))
        pattern2 = re.compile(r'target="_blank">(.*?)</a>',re.S)
        result2 = pattern2.findall(str(cd))
        #print (result1)
        urllist.extend(result1)
        #print (result2)
        titlelist.extend(result2)


        list1.remove(i)



    except:
        pass



# 多線程
def multithreading():
    sum=0

    while len(list1)>0:
        with ThreadPoolExecutor(max_workers=10) as executor:
            for result in executor.map(do, list1):
                sum+=1

    return sum




#list1=list(range(1,393,1))
list1=list(range(392,393,1))

sum=multithreading()
print ('還剩下{}頁'.format(list1))



end = time.clock()  # 計時-結束
print (("爬取完成 用時:"))
print ((end - start))


print ('總爬取 %d 頁 '%(sum))

while None in titlelist:
    titlelist.remove(None)

while None in urllist:
    urllist.remove(None)

#print (titlelist)

#print (urllist)


'''
#可作爲TXT輸出
with open("test.txt","w") as f:
        for thing in urllist:

            f.write(thing)
            f.write('\r\n')
'''





def getcontent(url,j):
    try:
        print (listj)

        html=requests.get(url,headers=header)

        html.encoding='gbk'

        Soup = BeautifulSoup(html.text, "lxml")

        con=Soup.select('div.art_contextBox ')

        cont=''
        for y in con:
            #print (type(str(y)))
            cont=cont+str(y)

        #print (cont)
        #print (j)
        listj.remove(j)
        #print ('****')
        #print (listj)
        return (cont)





    except:
        pass


def wpsend(title,content):

    wp = Client('http://www.6324.xyz/xmlrpc.php', '你的用戶名', '你的密碼')

    #print (content)
    post = WordPressPost()
    post.title = title
    #post.content = " ''' "+ content +" ''' "
    post.content = "  "+ str(content) +"  "
    post.post_status = 'publish'
    post.terms_names = {
       'post_tag': ['操盤策略'],
      'category': [ '期貨']
    }
    wp.call(NewPost(post))
    localtime = time.localtime(time.time())
    print ('文檔已上傳,執行時間 {}'.format(time.strftime("%Y-%m-%d %H:%M:%S",localtime)))


def work(j):

    url=urllist[j]
    title=titlelist[j]

    cont=getcontent(url,j)
    wpsend(title,cont)

    print ('成功完成任務採集任務第 {}號任務'.format(j))






# 多線程
def multithreading_con():
    sum=0
    global listj
    listj=list(range(len(urllist)))
    #print (type(listj))
    #print (listj)

    while len(listj)>0:
        with ThreadPoolExecutor(max_workers=5) as executor:
            for result in executor.map(work,listj ):
                sum+=1

    return sum



multithreading_con()

end = time.clock()  # 計時-結束
print ("全部上傳完成 用時:")

print ((end - start))

B站 演示視頻: https://www.bilibili.com/video/av80154643/

網站戰士: www.6324.xyz

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章