Import Multiprocessing,requests,time,re,os
"""
In fact, in my project and multithreading almost, multi-process multiple processes do not share global variables
So that means I just need a global variable to
"""
Package_list = []
# 1. URL of each page from 1-531, use for loop stitching URL
# 2. After opening the URL, use regular findall to crawl the specific package link of the page, and deposit the Package_list
def get_pic_url (page):
Try
Os.mkdir (str (page))
Except Exception as E:
Pass
Head = {
' User-agent ': ' mozilla/5.0 (Windows NT 10.0; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/61.0.3163.91 safari/537.36 '}
url = ' http://www.doutula.com/article/list/?page={} '. Format (page)
res = requests.get (url,headers = head)
TMP = Re.findall (R ' <a href= "([^#].*?)" class= ". *?" > ', Res.text)
Print (TMP)
Package_list.extend (TMP)
# 3. Use the For loop to traverse Package_list as Picture_url
# 4. Open the change URL and crawl the emoticons in the URL into a local folder or database
For Pic_url in Package_list:
If Len (pic_url)! = Len (' http://www.doutula.com/article/detail/1070805 '):
Break
Else
Res_pic = requests.get (pic_url,headers = head)
reg = R ' ' Reg = Re.compile (reg,re. S
TMP = Re.findall (reg,res_pic.text)
Print (TMP)
For I in TMP:
num = Tmp.index (i)
Picture_res = requests.get (i[0],headers = head)
string = ' '
If Picture_res:
TMP_STR = i[1]
For each in TMP_STR:
If each in (' \ \ ', '/', ' * ', '? ', ' ', ' | ', ' > ', ' < '):
Pass
Else
String = string + each
Tmp_str = string
If tmp_str[-3:] = = ' jpg ':
With open (R './{}/{}-{}{}.jpg '. Format (Page,package_list.index (Pic_url), num,tmp_str), ' WB ') as F:
F.write (picture_res.content)
Time.sleep (1)
Else
With open (R './{}/{}-{}{}.gif '. Format (Page,package_list.index (Pic_url), num,tmp_str), ' WB ') as F:
F.write (picture_res.content)
Time.sleep (1)
Else
Break
Package_list.clear ()
Def fun1 ():
For page in range (1,265):
Get_pic_url (page)
Def fun2 ():
For page in range (265,532): # Altogether 532
Get_pic_url (page)
if __name__ = = "__main__":
T1 = multiprocessing. Process (TARGET=FUN1)
T2 = multiprocessing. Process (TARGET=FUN2)
T1.start ()
T2.start ()
# Successful completion
Small Reptile Program Process version