eight_snakes

week5_final.py

Oct 16th, 2021 (edited)
450
138 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import re
  2. from urllib.request import urlopen
  3. from multiprocessing import Pool, Process
  4.  
  5.  
  6. input_str = input()
  7. input_tokens = input_str.split()
  8. source_url, thread_n = input_tokens[0], int(input_tokens[1])
  9.  
  10. resp = urlopen(source_url)
  11. html = resp.read().decode()
  12.  
  13. p_pages = re.compile(r'Page \d of (\d+)')
  14. n_pages = int(p_pages.search(html).groups()[0])
  15.  
  16. p = re.compile(r'(Django\s2\.0)')
  17.  
  18. def get_req(url):
  19.     resp = urlopen(url)
  20.     html = resp.read().decode()
  21.     return len(p.findall(html))
  22.  
  23. pool=Pool(thread_n)
  24. urls=['{}?page={}'.format(source_url, i) for i in range(1, n_pages)]
  25.  
  26. res = pool.map(get_req, urls)
  27.    
  28. print(sum(res))
  29.  
RAW Paste Data