Python (Requests and lxml Packages) - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Saturday 23 August 2014

Python (Requests and lxml Packages)


Requests Package in Python:

Following Steps will guide you how to Download and Setup the Packages in Python


Download at: https://github.com/kennethreitz/requests
Unzip the Downloaded file form above and copy the requests folder to C:\Python27\Lib

OR

Download setuptools-0.8.tar.gz file from the link: https://pypi.python.org/pypi/setuptools/0.8

Go to your Downloaded Location: C:\Users\neeraj\Downloads\setuptools-0.8
In CMD Go to your Downloaded Location of Setup tools and type: python easy_install.py

Now Go to C:\Python27\Scripts, you will Find 'easy_install' exe file.

Now To Install any package in python Go to CMD and browse to Location C:\Python27\Scripts and type 'easy_install requests'

To Install lxml type: easy_install lxml


lxml provides a very simple and powerful API for parsing XML and HTML. It supports one-step parsing as well as step-by-step parsing using an event-driven API (currently only for XML).Requests allow you to send HTTP/1.1 requests. You can add headers, form data, multipart files, and parameters with simple Python dictionaries, and access the response data in the same way.


Now You can Import Requests Package in your Python Code.

Sample Code

import requests
import urllib, urllib2

import xml.etree.ElementTree as ET

print "\nHello Neeraj, Welcome to the world of python\n\n"

x = input('Enter value of x\n')

if x==1:
print "you speak truth\n"
else:
print "try one more time may be u r lying\n"

list=["hello","name"]
print "List names are ",list

r = requests.get('https://github.com/timeline.json')
print r.status_code

if r.status_code==200:
 print "Website is up"
else:
 print "Website is down"

 r = requests.get('https://github.com')
print r.status_code

if r.status_code==200:
 print "Website is up"
else:
 print "Website is down"

tree = ET.parse('C:\\Users\\neeraj\\Desktop\\xml.xml')
root = tree.getroot()

for child in root:
 print child.tag, child.attrib

 tree.find('.//newtag').text = 'hello'
tree.write('C:\\Users\\neeraj\\Desktop\\xml.xml')



Note:  #! --->It is called the shebang
line - whenever the first two characters of the source file are #! followed by the location of a program,
this tells your Linux/Unix system that this program should be run with this interpreter when you execute
the program

No comments:

Post a Comment