使用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

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值