ThatOnePythonNoob ThatOnePythonNoob - 2 months ago 16
Python Question

How to search a search field on a webpage?

How can I open a webpage, for example: http://www.futbin.com/

... And search for an item using the search field on the page?

i.e. "Search for a player..."

Answer

You don't need to scrape the webpage, it has a direct search API

import requests
s = "Ronaldo"
r = requests.get("http://www.futbin.com/api?term={}".format(s))
r.json()

Outputs

[{u'club_image': u'/content/fifa16/img/clubs/243.png',
  u'full_name': u'Cristiano Ronaldo',
  u'id': u'38387',
  u'image': u'/content/fifa16/img/players/20801.png',
  u'nation_image': u'/content/fifa16/img/nation/38.png',
  u'position': u'LW',
  u'rare': u'1',
  u'rare_type': u'11',
  u'rating': u'99'},
...

Edit

from bs4 import BeautifulSoup
import requests

r = requests.get("http://www.futbin.com/16/player/38387/cristiano-ronaldo")
soup = BeautifulSoup(r.text, "html.parser")
pslbin = soup.findAll("span", {'id': "pslbin"})[0].text.strip()
print(plsbin)