-1

I'm new to Python and I'm currently working on solving problems to improve my coding skills. There is a webpage where I need to post data like Name, Language (this a drop down list), Phone number and Feedback. My idea here is to automate this process using a python script. Please find the code and details below:

import urllib2
import requests
url = "http://example.com/feedback_details"

try:
    with open('review.txt', 'r') as myfile:
        data1=myfile.read()
    payload = {'Name:': '200272YV', 
               'Language:': 'Python 2.7', 
               'Phone Number:': '123456789', 
               'Feedback (not more than 200 characters):' = 'data1'
              }
    requests.post(url, data=payload)

except urllib2.HTTPError, err:
    print err.fp.read()
    print "Page Not Found"

I have written a feedback in a text document called review.txtand I have read the whole contents in that file to a variable called data1. So now I want to give this variable name in the 'Feedback (not more than 200 characters):' = as data so that the whole content in the file can be related to this. But I'm not able to assign as variable. How can I send the complete contents in the text file and assign it to the payload. Also, how can i check if this posting is done successfully or not. Can someone tell me where am I doing wrong. Is my approach wrong or flow is wrong. Thanks in advance. Any help would be much appreciated.

sdgd
  • 723
  • 1
  • 17
  • 38
  • *But I'm not able to assign as variable* - Why can't you assign it as a variable? – Brendan Abel Feb 24 '16 at 18:29
  • when I try to assign `'Feedback (not more than 200 characters):' = data1`, it throws an invalid syntax error – sdgd Feb 24 '16 at 18:39
  • I'm not sure if i can assign it this way : `'Feedback (not more than 200 characters):' = 'data1'` as it will take the answer as data1 and not the contents of the file. – sdgd Feb 24 '16 at 18:41
  • 2
    It *is* invalid syntax. You have an equality sign instead of a colon. – kichik Feb 24 '16 at 18:41
  • @kichik colon ? where ? – sdgd Feb 24 '16 at 18:42
  • 1
    `'Feedback (not more than 200 characters)': = data1` – tdelaney Feb 24 '16 at 18:43
  • You accidentally put the colon inside the quoted string. – tdelaney Feb 24 '16 at 18:43
  • that colon is a part of the statement in the website. `'Feedback (not more than 200 characters):'` and then there is a box where i can enter the text and which is what i want to automate – sdgd Feb 24 '16 at 18:45
  • 1
    It should be `'Feedback (not more than 200 characters):' : data1`, You use a colon for dictionaries, not equal signs – Brendan Abel Feb 24 '16 at 18:46
  • thanks all. Understood my mistake and i have corrected it. How do i know if the request has been submitted ? there is submit button that i haven't clicked yet. – sdgd Feb 24 '16 at 18:50

1 Answers1

1

The correct syntax is:

import urllib2
import requests
url = "http://example.com/feedback_details"

try:
    with open('review.txt', 'r') as myfile:
        data1=myfile.read()
    payload = {'Name:': '200272YV', 
               'Language:': 'Python 2.7', 
               'Phone Number:': '123456789', 
               'Feedback (not more than 200 characters)': data1
              }
    requests.post(url, data=payload)

except urllib2.HTTPError, err:
    print err.fp.read()
    print "Page Not Found"
kichik
  • 33,220
  • 7
  • 94
  • 114
  • Thank you. got to know my mistake. there is a submit button that i havent clicked , but will `requests.post(url, data=payload)` help in submitting the data and going to the next page ? – sdgd Feb 24 '16 at 18:50