Created
April 12, 2018 15:02
-
-
Save 1328/d0d8b330506e7554140c13875abd8970 to your computer and use it in GitHub Desktop.
weapon grabber
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup | |
import sqlite3 | |
import sys | |
import time | |
GROUPS = { | |
'Grenade':(52,55), | |
'Other':(55,56), | |
'Crossbow':(42,44) | |
} | |
def check(text): | |
if text.startswith('egg'): | |
return False | |
if text in [' ', '0%', '???']: | |
return False | |
return True | |
def parse_table(soup) | |
for table in soup.find_all('table', class_="wikitable")[1:-1]: | |
for tr in table.find_all('tr'): | |
stats = [] # Stats for current weapon | |
for td in tr.find_all("td"): | |
text = td.text | |
if not(check(text)): | |
continue | |
stats.append(text) | |
if len(stats)>1: | |
yield stats | |
def parse_page(soup): | |
weapons = [weapon for weapon in parse_table(soup)] | |
return weapons | |
def grab_page(): | |
"""Pull Weapon Stat info | |
Returns list of weapons and corresponding stats""" | |
connection_error = True | |
retry = 0 | |
for count in range(5) | |
try: | |
r = requests.get('http://orcz.com/Fortnite_Battle_Royale:_Weapons') | |
soup = BeautifulSoup(r.content, 'lxml') | |
return soup | |
except requests.exceptions.RequestException as e: | |
print(e) | |
print("Error with internet connection." | |
"\nCheck connection and try again.") | |
print("Error: Max retries exceeded") | |
return None | |
def create_table(cursor, table_name): | |
slot10 = '''Weapon TEXT, | |
Rarity TEXT, | |
Dps TEXT, | |
Damage TEXT, | |
FireRate TEXT, | |
MagSize TEXT, | |
ReloadTime TEXT, | |
AmmoCost TEXT, | |
HeadShotDamage TEXT, | |
StructureDamage TEXT''' | |
cursor.execute('DROP TABLE IF EXISTS {}'.format(table_name)) | |
cursor.execute('CREATE TABLE {}({})'.format( | |
table_name, | |
slot10)) | |
def insert(cursor, table, data) | |
cursor.execute('''INSERT INTO {}(Weapon, Rarity, Dps, Damage, FireRate, MagSize, | |
ReloadTime, AmmoCost, HeadShotDamage, StructureDamage) | |
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''', (*data,)) | |
def store_weapons(weapons) | |
# consider using with | |
con = sqlite3.connect('Weapons.DB') | |
cur = con.cursor() | |
for weapon_name, (start, stop) in GROUPS.items(): | |
create_table(cur, weapon_name) | |
for weapon in weapons[start:stop]: | |
insert(cur, weapon_name, weapon) | |
con.commit() | |
con.close() | |
def main(): | |
soup = grab_page() | |
weapons = parse_page() | |
store_weapons(weapons) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment