Chase Sariaslani
Chase Sariaslani

Reputation: 75

Fetch Table from Website using BeautifulSoup

Using Python, I am trying to scrape a website and I am attempting to grab some values. In this case, I want to grab a table. Here is the specific site in question:

http://wotvffbe.gamea.co/c/5vdp3v91

When attempting to scrape it, I am trying to grab these values in the data table:

enter image description here

I am using BeautifulSoup to sift through the values. I would like a way to grab them in a way such that it spots them by some form of reference. I was able to grab these values before, but when moving to the next site, they are not in the same position. Hence, I would like a way to spot it by reference rather than position.

Thank you for your input.

Also, if you would like some other sites to test, here are the ones I am testing:

http://wotvffbe.gamea.co/c/v89gxxuy

http://wotvffbe.gamea.co/c/yhb5ucqz

http://wotvffbe.gamea.co/c/yju5zfhe

Upvotes: 1

Views: 38

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195418

import requests
from bs4 import BeautifulSoup

url = 'http://wotvffbe.gamea.co/c/5vdp3v91'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')

table = soup.select_one('th:contains("Cost")').find_parent('table')
d = dict([(th.text, td.text) for th, td in zip(table.select('th'), table.select('td'))])

# pretty print it to screen:
from pprint import pprint
pprint(d)

Prints:

{'AP': '110',
 'Attack': '225',
 'Cost': '80',
 'Dexterity': '168',
 'HP': '2079',
 'Jump': '2',
 'Luck': '149',
 'Magic ': '64',
 'Move': '3',
 'Range': '1',
 'Speed': '62',
 'TP': '117'}

Upvotes: 1

Related Questions