Extracting items out of an element.ResultSet Extracting items out of an element.ResultSet python-3.x python-3.x

Extracting items out of an element.ResultSet


Just use a list comprehension.

resultSet = soup.find_all("div", attrs = {"class": "tp-title"})stats = [    (i.contents[0], i.contents[1].text) for i in resultSet]

Or, a for loop.

stats = []for i in resultSet:    stats.append(i.contents[0], i.contents[1].text)

print(stats)[    ('40 Yard Dash', '4.44 Secs'),    ('3 Cone Drill', '6.97 secs'),    ('Broad Jump', '118.0 inches'),    ('20 Yard Shuttle', '4.18 secs'),    ('Vertical Jump', '34.5 inches')]


This is another approach which does the same thing. Slightly awkward to look at, though.

import requestsfrom bs4 import BeautifulSoupURL = "http://www.nfl.com/player/deandrewwhite/2552657/combine"res = requests.get(URL)soup = BeautifulSoup(res.text,"lxml")items = {item.select_one(".tp-results").previous_sibling:item.select_one(".tp-results").text for item in soup.select(".tp-title")}print(items)

Output:

{'3 Cone Drill': '6.97 secs', '20 Yard Shuttle': '4.18 secs', '40 Yard Dash': '4.44 Secs', 'Vertical Jump': '34.5 inches', 'Broad Jump': '118.0 inches'}