Sending multiple POST data in Python
Question: I have a Python code that sends POST request to a website, reads the response
and filters it. For the POST data I used ('number', '11111') and it works
perfect. However, I want to create a txt file that contains 100 different
numbers as 1111,2222,3333,4444... and then send the POST requests for each of
them. Can you help me how to do this in Python?

    
    
    import urllib
    from bs4 import BeautifulSoup
    
    headers = {
        'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Origin': 'http://mahmutesat.com/python.aspx',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML, like Gecko)  Chrome/24.0.1312.57 Safari/537.17',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Referer': 'http://mahmutesat.com/python.aspx',
        'Accept-Encoding': 'gzip,deflate,sdch',
        'Accept-Language': 'en-US,en;q=0.8',
        'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
    }
    
    class MyOpener(urllib.FancyURLopener):
        version = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17'
    
    myopener = MyOpener()
    url = 'http://mahmutesat.com/python.aspx'
    # first HTTP request without form data
    f = myopener.open(url)
    soup = BeautifulSoup(f)
    # parse and retrieve two vital form values
    viewstate = soup.select("#__VIEWSTATE")[0]['value']
    eventvalidation = soup.select("#__EVENTVALIDATION")[0]['value']
    viewstategenerator = soup.select("#__VIEWSTATEGENERATOR")[0]['value']
    
    formData = (
        ('__EVENTVALIDATION', eventvalidation),
        ('__VIEWSTATE', viewstate),
        ('__VIEWSTATEGENERATOR',viewstategenerator),
        ('number', '11111'),
        ('Button', 'Sorgula'),   
    )
    
    encodedFields = urllib.urlencode(formData)
    # second HTTP request with form data
    f = myopener.open(url, encodedFields)
    
    soup = BeautifulSoup(f.read())
    
    name=soup.findAll('input',{'id':'name_field'})
    
    for eachname in name:
       print eachname['value']


Answer: 1 - Here is an example on how to create a file:

    
    
    f = open('test.txt','w')
    

This will open the `test.txt` file for writing (`'w'`) (if it has already
data, it will be erased but if you want to append it write: `f =
open('test.txt','a')` ) or create one if it does not exist yet. Note that this
will happen in your current working directory, if you want it in a specific
directory, include with the file name the full directory path, example:

    
    
    f = open('C:\\Python\\test.txt','w')
    

2 - Then write/append to this file the data you want, example:

    
    
    for i in range(1,101):
         f.write(str(i*1111)+'\n')
    

This will write 100 numbers as string from 1111 to 111100

3 - You should always close the file at the end:

    
    
    f.close()
    

4 - Now if you want to read from this file '`test.txt`':

    
    
    f = open('C:\\Python\\test.txt','r')
    for i in f:
    print i,
    file.close()
    

This is as simple as it can be,

You need to read about File I/O in python from:

<https://docs.python.org/2.7/tutorial/inputoutput.html#reading-and-writing-
files>

Make sure you select the right Python version for you in this docs.

