本文实例讲述了python多进程方式抓取基金网站内容的方法。分享给大家供大家参考,具体如下:
在前面这篇http://www.zzvips.com/article/174768.html我们已经简单了解了”python的多进程”,现在我们需要把抓取基金网站(28页)内容写成多进程的方式。
因为进程也不是越多越好,我们计划分3个进程执行。意思就是 :把总共要抓取的28页分成三部分。
怎么分呢?
1
2
3
4
5
6
|
# 初始range r = range ( 1 , 29 ) # 步长 step = 10 mylist = [r[x:x + step] for x in range ( 0 , len (r),step)] print (mylist) # [range(1, 11), range(11, 21), range(21, 29)] |
看上面代码,我们就把1~29分成了三部分,list里三个range。
2、还记得我们抓取基金网站内容的getdata()
函数?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
def getdata(start, end): for x in range (start, end + 1 ): # 去第几页 输入框 tonum = driver.find_element_by_id( "tonum" ) # 去第几页 提交按钮 jumpbtn = driver.find_element_by_id( "btn_jump" ) tonum.clear() # 第x页 输入框 tonum.send_keys( str (x)) # 去第x页 jumpbtn.click() # 点击按钮 webdriverwait(driver, 20 ).until( lambda driver: driver.find_element_by_id( "pager" ) \ .find_element_by_xpath( "span[@value={0} and @class!='end page']" . format (x)) \ .get_attribute( "class" ).find( "at" ) ! = - 1 ) # 保存抓取到的html内容 # 保存到html目录下 with open ( "./htmls/{0}.txt" . format (x), "wb" ) as f: f.write(driver.find_element_by_id( "tablediv" ).get_attribute( "innerhtml" ).encode( "utf8" )) f.close() |
函数是有2个参数:一个开始页码,一个结束页码,也就是从第start页,到第end页。
这2个参数实际上也是range
。
改造getdata()
函数如下(参数不同了):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# 循环抓取网页内容的函数 def getdata(myrange): for x in myrange: # 去第几页 输入框 tonum = driver.find_element_by_id( "tonum" ) # 去第几页 提交按钮 jumpbtn = driver.find_element_by_id( "btn_jump" ) tonum.clear() # 第x页 输入框 tonum.send_keys( str (x)) # 去第x页 jumpbtn.click() # 点击按钮 webdriverwait(driver, 20 ).until( lambda driver: driver.find_element_by_id( "pager" ) \ .find_element_by_xpath( "span[@value={0} and @class!='end page']" . format (x)) \ .get_attribute( "class" ).find( "at" ) ! = - 1 ) # 保存抓取到的html内容 # 保存到html目录下 with open ( "./htmls/{0}.txt" . format (x), "wb" ) as f: f.write(driver.find_element_by_id( "tablediv" ).get_attribute( "innerhtml" ).encode( "utf8" )) f.close() |
3、创建进程,把target设置为上面的getdata()
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# 初始range r = range ( 1 , int (total_page) + 1 ) # 步长 step = 10 mylist = [r[x:x + step] for x in range ( 0 , len (r),step)] # 把页面分段 # 创建进程 processlist = [] if __name__ = = "__main__" : for r in mylist: p = process(target = getdata,args = (r,)) processlist.append(p) # 开始执行进程 for p in processlist: p.start() |
这样就有三个进程分别抓取。
4、多进程抓取基金网站多页内容完全代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# coding: utf-8 from selenium import webdriver from selenium.webdriver.support.ui import webdriverwait from selenium.webdriver.support import expected_conditions from multiprocessing import process driver = webdriver.phantomjs(executable_path = r "你phantomjs的可执行文件路径" ) # 请求一个网址 driver.get( "http://fund.eastmoney.com/fund.html" ) page_text = driver.find_element_by_id( "pager" ).find_element_by_xpath( "span[@class='nv']" ).text total_page = ''.join( filter ( str .isdigit,page_text)) # 得到总共有多少页 # 循环抓取网页内容的函数 def getdata(myrange): for x in myrange: # 去第几页 输入框 tonum = driver.find_element_by_id( "tonum" ) # 去第几页 提交按钮 jumpbtn = driver.find_element_by_id( "btn_jump" ) tonum.clear() # 第x页 输入框 tonum.send_keys( str (x)) # 去第x页 jumpbtn.click() # 点击按钮 webdriverwait(driver, 20 ).until( lambda driver: driver.find_element_by_id( "pager" ) \ .find_element_by_xpath( "span[@value={0} and @class!='end page']" . format (x)) \ .get_attribute( "class" ).find( "at" ) ! = - 1 ) # 保存抓取到的html内容 # 保存到html目录下 with open ( "./htmls/{0}.txt" . format (x), "wb" ) as f: f.write(driver.find_element_by_id( "tablediv" ).get_attribute( "innerhtml" ).encode( "utf8" )) f.close() # 初始range r = range ( 1 , int (total_page) + 1 ) # 步长 step = 10 mylist = [r[x:x + step] for x in range ( 0 , len (r),step)] # 把页面分段 # 创建进程 processlist = [] if __name__ = = "__main__" : for r in mylist: p = process(target = getdata,args = (r,)) processlist.append(p) # 开始执行进程 for p in processlist: p.start() |
希望本文所述对大家python程序设计有所帮助。
原文链接:https://blog.csdn.net/github_26672553/article/details/78616084