Kosty_Fomin

HSEMath

Feb 28th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | None | 0 0
  1. Задача 3 (2 балла)
  2. def any_news_about_harry(url):
  3.     import urllib.request
  4.     try:
  5.         f = urllib.request.urlopen(url)
  6.         if "Harry" in str(f.read()):
  7.             return True
  8.         else:
  9.             return False
  10.     except:
  11.         False
  12. Задача 2 (1 балл)
  13. def get_strong(html):
  14.     import bs4
  15.     soup = bs4.BeautifulSoup(html, "html.parser")
  16.     return soup.strong.text
  17. Задача 3 (1 балл)
  18. def all_images_src(xml):
  19.             import bs4
  20.             soup = bs4.BeautifulSoup(xml, "html.parser")
  21.             res = []
  22.             for a in soup.find_all(['img']):
  23.                 res.append(a['src'])
  24.             return res
  25. Задача 4 (2 балла)
  26. from urllib.parse import urlencode
  27. entrypoint = "https://ru.wikipedia.org/w/index.php?"
  28. def mkurl(title, oldid):
  29.     return entrypoint+urlencode(dict(title=title, oldid=oldid))
  30.  
  31. def get_all_headings(url):
  32.     import bs4
  33.     import urllib.request
  34.     res = []
  35.     try:
  36.         f = urllib.request.urlopen(url)
  37.         soup = bs4.BeautifulSoup(f.read(), "html.parser")
  38.         s = None
  39.         for a in soup.find_all(['span'], {'class': 'mw-headline'}):
  40.              if 'h2'in str(a.parent):
  41.                  res.append(a.parent.find(['span'], {'class': 'mw-headline'}).text)
  42.         return res
  43.     except:
  44.         return ["Not found"]
  45.  
  46. Задача 5 (3 балла)
  47. from urllib.parse import urlencode
  48. entrypoint = "https://ru.wikipedia.org/w/index.php?"
  49. def mkurl(title):
  50.     return entrypoint+urlencode(dict(title=title))
  51.  
  52. def city_site(name):
  53.     url = mkurl(name)
  54.     import bs4
  55.     import urllib.request
  56.  
  57.     try:
  58.         f = urllib.request.urlopen(url)
  59.         soup = bs4.BeautifulSoup(f.read(), "html.parser")
  60.  
  61.         for a in soup.find_all(['tr']):
  62.             buferVar = a.find('th')
  63.             if buferVar != None and buferVar.text == "Официальный сайт":
  64.                 return a.find(['a'], {'class': 'external text'})['href']
  65.  
  66.     except:
  67.         return None
  68.  
  69.  
  70.  
  71.  
  72. Задача 6 (4 балла)
  73.  
  74. def get_languages_hse(last_name):
  75.     def get_teacher_url(last_name):
  76.         url = 'https://www.hse.ru/org/hse/ouk/hmat/persons'
  77.         import bs4
  78.         import urllib.request
  79.  
  80.         try:
  81.             f = urllib.request.urlopen(url)
  82.             soup = bs4.BeautifulSoup(f.read(), "html.parser")
  83.             s = None
  84.             for a in soup.find_all(['a'], {'class': 'fa-person__name'}):
  85.                 if last_name in a.text:
  86.                     return 'http://' + a['href'][2:]
  87.         except:
  88.             return None
  89.     # coding=utf8
  90.     url = get_teacher_url(last_name)
  91.  
  92.     import bs4
  93.     import urllib.request
  94.     res = []
  95.  
  96.     try:
  97.         f = urllib.request.urlopen(url)
  98.         soup = bs4.BeautifulSoup(f.read(), "html.parser")
  99.         for a in soup.find_all(['dl'],{'class':'main-list large main-list-language-knowledge-level'}):
  100.             for r in a.find_all('dd'):
  101.                 res.append(r.text)
  102.             return res
  103.     except:
  104.         pass
Add Comment
Please, Sign In to add comment