I have a webpage which contains one or more forms. What i want to do is:
- Identify the forms
- Send post requests and catch the response.
I'm over point 1, i'm using requests.get and Beautifulsoup to identify the forms from the webpage. My question is, how can i get the form url without submitting the form?
Example: I'll search for "test" on https://stackoverflow.com/
The url looks like this: https://stackoverflow.com/search?q=test
I'm interested in getting this part: /search?q because other sites have more complicated urls in these cases and i want to build a scraper that is not website-dependent.
The full code which i tried:
from bs4 import BeautifulSoup
import urllib.request
import requests
import mechanicalsoup
#### What?
search_words=['search1','search2']
website='http://www.website.com/'
####
s=requests.Session()
r=s.get(website)
soup_main = BeautifulSoup(r.content,'lxml')
form=soup_main.find('form')
print(form)
param={'searchword':search_words[0]}
method = str(form.get("method"))
print(method)
action =form.get("action")
url = urllib.parse.urljoin(website, action)
print(action)
request1=requests.Request(method,url,params=param)