博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
之前用的一个多进程python爬虫
阅读量:6216 次
发布时间:2019-06-21

本文共 2823 字,大约阅读时间需要 9 分钟。

版权声明:本文可能为博主原创文章,若标明出处可随便转载。 https://blog.csdn.net/Jailman/article/details/77573959
#!C:\Python27\python.exe#coding=utf8import osimport urllibimport urllib2from bs4 import BeautifulSoupfrom multiprocessing import Poolimport socketsocket.setdefaulttimeout(60)def url_open(url):    user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36'    headers = {'User-Agent': user_agent}    request = urllib2.Request(url=url, headers=headers)    try:        page = urllib2.urlopen(request, timeout=60)    except urllib2.HTTPError as e:        return 1    contents = page.read()    # print contents    soup = BeautifulSoup(contents.decode('gb2312','ignore'), "lxml")    return soupdef retrieve_img(dir, link):    if url_open(link) != 1:        soup = url_open(link)        for line in soup.find_all("div", "ContentBox"):            img_list = line.find_all('img')            for img_item in img_list:                raw_img = img_item.get('src')                img_url = 'http:' + raw_img                img_name = raw_img.split('/')[-1]                print "Download: %s" % img_url                # print img_name                urllib.urlretrieve(img_url, dir + '\\' + img_name)def crawler(root, url):        # print url        if url_open(url) != 1:            soup = url_open(url)            # print soup            for line in soup.find_all("div", "box_con newslist"):                li = line.find_all('li')                for a in li:                    href = a.find_all('a')                    link = root + href[0].get('href')                    text = href[0].get_text()                    # print link                    print "Creating directory %s..." % text                    if not os.path.isdir(text):                        try:                            os.mkdir(text)                            if os.path.isdir(text):                                print "Directory %s successfully created!" % text                                print "Crawling image page %s." % link                                retrieve_img(text, link)                        except IOError, e:                            print e                    elif os.path.isdir(text):                        print "Directory %s already exists!" % text                        print "Crawling image page %s." % link                        retrieve_img(text, link)                    # break #func testdef single_func(num):    root = 'http://xxx.com'    url = "http://xxx.com/articlelist/?20-" + str(num) + '.html'    crawler(root, url)if __name__ == '__main__':    # single_func(1) #func test    pool = Pool(processes=8)    for i in range(1, 187):        result = pool.apply_async(single_func, (i,))    pool.close()    pool.join()    if result.successful():        print 'Successful!'
你可能感兴趣的文章
JDBC的事务
查看>>
linux服务器CPU参数/proc/cpuinfo
查看>>
haystack+Elasticsearch搜素引擎
查看>>
UEFI系统安装U盘的制作方式
查看>>
读《Oracle DBA工作笔记》知识点-获取创建语句
查看>>
Io流的概述
查看>>
js功能实现top轮播图
查看>>
App 卸载记录
查看>>
TCAM 与CAM
查看>>
POJ 3667 & HDU 3308 & HDU 3397 线段树的区间合并
查看>>
JQuery 的Ajax的使用
查看>>
jmeter设置json断言
查看>>
浅析C#代理
查看>>
android ListView下拉刷新之功能实现
查看>>
逆天的H3C Comware V7 RBAC
查看>>
ThinkPad 预装win8换win7(软激活)
查看>>
个人或小型企业站该如何选择服务器?
查看>>
属性选择器
查看>>
hdu----(2586)How far away ?(DFS/LCA/RMQ)
查看>>
jQuery数组处理汇总
查看>>