Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.84 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup as bs
  3. import mysql.connector
  4.  
  5.  
  6. mydb = mysql.connector.connect(
  7.     host='localhost',
  8.     user='root',
  9.     passwd='1',
  10.     database='scraping'
  11. )
  12.  
  13. def get_html(url):
  14.     r = requests.get(url)
  15.     return r.text
  16.  
  17.  
  18. def get_page_data(html):
  19.     soup = bs(html, 'lxml')
  20.     name = soup.find('div', id='meta').find('h1').text
  21.     print(name)
  22.     position = soup.find('div', id='meta').find('p').find('strong', text='Position')
  23.     try:
  24.         print(position.next_sibling[2:9].replace('•',''))
  25.     except:
  26.         position =''
  27.     hand = soup.find('div', id='meta').find('p').find('strong', text='Shoots')
  28.     try:
  29.         print(hand.next_sibling[2:8])
  30.     except:
  31.         hand = ''
  32.     height = soup.find('div', id='meta').find_all('p')[1].find_all('span')[1]
  33.     print(height.next_sibling[2:7])
  34.  
  35.     weight = soup.find('div', id='meta').find_all('p')[1].find_all('span')[1]
  36.     print(weight.next_sibling[9:].replace(')',''))
  37.  
  38.     born = soup.find('div', id='meta').find('span', id='necro-birth').get('data-birth')
  39.     print(born)
  40.  
  41.     city_born = soup.find('div', id='meta').find('span', itemprop='birthPlace').find('a')
  42.     try:
  43.         print(city_born.previous_sibling.replace(',', '').lstrip())
  44.     except:
  45.         city_born = ''
  46.  
  47.     country_born = soup.find('div', id='meta').find('span', itemprop='birthPlace').find('a')
  48.     try:
  49.         print(country_born.text.replace('California', 'US').
  50.                                 replace('Michigan', 'US').
  51.                                 replace('Florida', 'US').
  52.                                 replace('Texas', 'US').
  53.                                 replace('Washington', 'US').
  54.                                 replace('Hawaii', 'US').
  55.                                 replace('New York', 'US').
  56.                                 replace('Arizona', 'US').
  57.                                 replace('Washington', 'US').
  58.                                 replace('Georgia', 'US').
  59.                                 replace('Michigan', 'US').
  60.                                 replace('New Jersey', 'US').
  61.                                 replace('Pennsylvania', 'US').
  62.                                 replace('Alabama', 'US').
  63.                                 replace('Virginia', 'US').
  64.                                 replace('Illinois', 'US').
  65.                                 replace('Massachusetts', 'US').
  66.                                 replace('Colorado', 'US').
  67.                                 replace('Minnesota', 'US').
  68.                                 replace('Ohio', 'US').
  69.                                 replace('Alaska', 'US').
  70.                                 replace('Tennessee', 'US').
  71.                                 replace('Oregon', 'US').
  72.                                 replace('Missouri', 'US').
  73.                                 replace('Maryland', 'US').
  74.                                 replace('South Carolina', 'US').
  75.                                 replace('Indiana', 'US').
  76.                                 replace('Wisconsin', 'US').
  77.                                 replace('Utah', 'US').
  78.                                 replace('Kentucky', 'US').
  79.                                 replace('Louisiana', 'US').
  80.                                 replace('Connecticut', 'US').
  81.                                 replace('Oklahoma', 'US').
  82.                                 replace('Nevada', 'US').
  83.                                 replace('Maine', 'US').
  84.                                 replace('Mississippi', 'US').
  85.                                 replace('New Mexico', 'US').
  86.                                 replace('Kansas', 'US').
  87.                                 replace('Iowa', 'US').
  88.                                 replace('Montana', 'US').
  89.                                 replace('Nebraska', 'US').
  90.                                 replace('Arkansas', 'US').
  91.                                 replace('Wyoming', 'US').
  92.                                 replace('Rhode Island', 'US').
  93.                                 replace('New Hampshire', 'US').
  94.                                 replace('Delaware', 'US').
  95.                                 replace('West Virginia', 'US').
  96.                                 replace('Vermont', 'US').
  97.                                 replace('Idaho', 'US').
  98.                                 replace('North Dakota', 'US').
  99.                                 replace('South Dakota', 'US'))
  100.  
  101.     except:
  102.         country_born = ''
  103.  
  104.     round_draft = soup.find('div', id='meta').find_all('p')[3:5]
  105.     for i in round_draft:
  106.         try:
  107.             rd = i.find('a')
  108.             print(rd.next_sibling.replace('\\','').replace(',', '').replace(')','').replace('(','').lstrip())
  109.         except:
  110.             rd = ''
  111.  
  112.     twitter_link = soup.find('div', id='meta').find('a', rel='nofollow')
  113.     try:
  114.         print(twitter_link.get('href'))
  115.     except:
  116.         twitter_link =''
  117.  
  118.     link_pic = soup.find('div', id='meta').find('div', class_='media-item')
  119.     try:
  120.         print(link_pic.find('img').get('src'))
  121.     except:
  122.         link_pic = ''
  123.  
  124.  
  125.  
  126.  
  127.  
  128.     mycursor = mydb.cursor()
  129.     sqlFormula = 'INSERT INTO skate_stat (name) VALUES (%s),'
  130.     mycursor.execute(sqlFormula, (namelastname,))
  131.  
  132.     mydb.commit()
  133.  
  134.  
  135. def main():
  136.     pattern = 'https://www.hockey-reference.com'
  137.     main = 'https://www.hockey-reference.com/players/'
  138.     for be_letter in ('a', 'b', 'h'):
  139.         letter = main + be_letter
  140.         print(letter)
  141.         soup = bs(get_html(letter), 'lxml')
  142.         searcher = soup.find('div', class_='section_content').find_all('p')
  143.         for i in searcher:
  144.             try:
  145.                 final = i.find('a').get('href')
  146.                 url = pattern + final
  147.                 print(url)
  148.                 get_page_data(get_html(url))
  149.             except:
  150.                 break
  151.  
  152.  
  153. if __name__ == '__main__':
  154.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement