jeudi 24 janvier 2019

scrape and post data in a single session using requests module

I need to write a python script to post data to a form but for that to happen i need to evaluate a randomly generated problem string like “10+21” and enter in one of the field.

when i scrape and post the sum using request.post() i am getting the page’s source code. i am guessing because the request.post() makes a fresh request due to which the problem string changes. how do i go about solving this? do i have to maintain sessions?

from lxml import html
import requests
import urllib
import urllib2
import requests

url='URL_HERE'
page=s.get(url)
tree=html.fromstring(page.content)
numbers=tree.xpath('//*[@id="envelope"]/form/label[2]')
num_str=numbers[0].text_content()
a=int(num_str[8])
b=int(num_str[12])
sum=a+b
values={'code':'ABCDEF1234', //web-form label names 
    'captcha':sum
    }

r=s.post(url,data=values)
print r.status_code,"-----",r.reason
print r.text




Aucun commentaire:

Enregistrer un commentaire