Python‎ > ‎

requests


Definition
Modules request the HTTP library.

PACKAGE CONTENTS
  • adapters
  • api
  • auth
  • certs
  • compat
  • cookies
  • exceptions
  • hooks
  • models
  • packages (package)
  • sessions
  • status_codes
  • structures
  • utils
Examples
The pyramid returns with the requests after finding the number of 'a' in www.pythonlake.com.
from wsgiref.simple_server import make_server
import requests

def find(request):
    r=requests.get('http://www.pythonlake.com')
    text=r.text
    f=text.find('a')
    f=str(f)
    response = Response()
    response.text=f
    return response
    
if __name__ == '__main__':
    config = Configurator()
    config.add_route('find', '/')
    config.add_view(find, route_name='find')
    app = config.make_wsgi_app()
    server = make_server('0.0.0.0', 6500, app)
    server.serve_forever()






The web contents are scrapped through BueatifulSoup4 through for loop.
 >>> from bs4 import BeautifulSoup
>>> import requests
>>> r=requests.get('http://www.pythonlake.com/contactus')
>>> b=BeautifulSoup(r.content)
>>> for link in b.find_all('a'):
...     link.get('href')
... 
'http://www.pythonlake.com/'
'/home'
'/aboutus'
'/download'
'/appointment'
'/Contactus'
'/system/app/pages/sitemap/hierarchy'
'/home'
'/beautifulsoup4'
'/boolean'
'/if'
'/while'
'/strisprintable'
'/strisnumeric'
'/strisalpha'
'/strisdigit'
'/setissuperset'
'/setisdisjoint'
'/setissubset'
'/decimalis_finite'
'/decimalis_normal'
'/decimalis_nan'
'/decimalis_nan'
'/decimalis_canonical'
'/decimalis_signed'
'/decimalis_subnormal'
'/decimalis_snan'
'/decimalis_infinite'
'/built-in'
'/abs'
'/bin'
'/bool'
'/bytearray'
'/chr'
'/complex'
'/countstr'
'/dir'
'/divmod'
'/enumerate'
'/float'
'/hex'
'/int'
'/input'
'/len'
'/max'
'/min'
'/ord'
'/range'
'/round'
'/pow'
'/print'
'/slice'
'/sorted'
'/sum'
'/type'
'/control-flow'
'/break'
'/continue'
'/next'
'/def'
'/except'
'/for'
'/if'
'/lambda'
'/pass'
'/while'
'/try'
'/dictionary'
'/dictclear'
'/dictcopy'
'/dictfromkeys'
'/dictget'
'/dictitems'
'/dictkeys'
'/dictpop'
'/dictpopitem'
'/dictsetdefault'
'/dictupdate'
'/dictvalues'
'/decimal'
'/decimaladjusted'
'/decimalas_tuple'
'/decimalcanonical'
'/decimalcompare'
'/decimalcompare_signal'
'/decimalcompare_total'
'/decimalcompare_total_mag'
'/decimalconjugate'
'/decimalcopy_abs'
'/decimalcopy_negate'
'/decimalcopy_sign'
'/decimalexp'
'/decimalfma'
'/decimalfrom_float'
'/decimalimag'
'/decimalis_canonical'
'/decimalis_finite'
'/decimalis_nan'
'/decimalis_normal'
'/decimalis_qnan'
'/decimalis_signed'
'/decimalsubnormal'
'/decimalis_zero'
'/decimalln'
'/decimallog10'
'/decimallogb'
'/decimallogical_and'
'/decimallogical_invert'
'/decimallogical_or'
'/decimallogical_xor'
'/decimalmax'
'/decimalmax_mag'
'/decimalmin_mag'
'/decimalnext_plus'
'/decimalnext_toward'
'/decimalnormalize'
'/decimalnumber_class'
'/decimalquantize'
'/decimalradix'
'/decimalreal'
'/decimalremainder_near'
'/decimalrotate'
'/decimalsame_quantum'
'/decimalscaleb'
'/decimalshift'
'/decimalsqrt'
'/decimalto_eng_string'
'/decimalto_integral'
'/decimalto_integral_exact'
'/decimalto_integral_value'
'/frozenset'
'/frozensetcopy'
'/frozensetdifference'
'/frozensetintersection'
'/frozensetisdisjoint'
'/frozensetissubset'
'/frozensetissuperset'
'/frozensetsymmetric_difference'
'/frozensetunion'
'/fractions'
'/fractionsfraction'
'/fractionsgcd'
'/fraction'
'/fractionconjugate'
'/fractiondenominator'
'/fractionfrom_decimal'
'/fractionfrom_float'
'/fractionimag'
'/fractionlimit_denominator'
'/fractionnumerator'
'/fractionreal'
'/integer'
'/int-bit_length'
'/int-conjugate'
'/int-denominator'
'/int-from_bytes'
'/intimag'
'/intnumerator'
'/intreal'
'/intto_bytes'
'/keywords'
'/and'
'/as'
'/assert'
'/break'
'/class'
'/continue'
'/del'
'/def'
'/else'
'/elif'
'/exec'
'/except'
'/finally'
'/for'
'/from'
'/global'
'/if'
'/import'
'/in'
'/is'
'/lambda'
'/not'
'/or'
'/pass'
'/print'
'/raise'
'/return'
'/try'
'/while'
'/with'
'/yield'
'/list'
'/listappend'
'/listclear'
'/listcount'
'/listcopy'
'/listextend'
'/listinsert'
'/listreverse'
'/listpop'
'/listsort'
'/itertools'
'/itertoolsaccumulate'
'/itertoolschain'
'/itertoolscombinations'
'/itertoolscombinations_with_replacement'
'/itertoolscompress'
'/itertoolscount'
'/itertoolscycle'
'/itertoolsdropwhile'
'/itertoolsfilterfalse'
'/itertoolsgroupby'
'/itertoolsislice'
'/itertoolspermutations'
'/itertoolsproduct'
'/itertoolsrepeat'
'/itertoolsstarmap'
'/itertoolstakewhile'
'/itertoolstee'
'/itertoolszip_longest'
'/math'
'/mathacos'
'/math-acosh'
'/mathasin'
'/mathasinh'
'/mathatan'
'/mathatan2'
'/mathatanh'
'/ceil'
'/mathcopysign'
'/mathcos'
'/mathcosh'
'/mathe'
'/matherf'
'/mathdegrees'
'/math-exp'
'/mathexpm1'
'/mathfabs'
'/math-factorial'
'/mathfloor'
'/mathfmod'
'/mathfrexp'
'/mathfsum'
'/mathgamma'
'/mathisfinite'
'/mathisinf'
'/mathldexp'
'/mathlog10'
'/mathlog1p'
'/mathlog'
'/mathlog2'
'/mathmodf'
'/mathpi'
'/mathpow'
'/mathradians'
'/mathsin'
'/mathsinh'
'/mathsqrt'
'/mathtan'
'/mathtanh'
'/mathtrunc'
'/operators'
'/operatorabs'
'/operatoradd'
'/operatorand_'
'/operatorconcat'
'/operatorcontains'
'/operatordelitem'
'/operatoreq'
'/operatorfloordiv'
'/operatorge'
'/operatorgetitem'
'/operatorgt'
'/operatoriadd'
'/operatoriconcat'
'/operatorifloordiv'
'/operatorilshift'
'/operatorimod'
'/operatorimul'
'/operatorindex'
'/operatorindexof'
'/operatorinv'
'/operatoripow'
'/operatorirshift'
'/operatoris_'
'/operatorisub'
'/operatoritruediv'
'/operatorixor'
'/operatorlength_hint'
'/operatorlshift'
'/operatormod'
'/operatormul'
'/operatorne'
'/operatornot_'
'/operatoror_'
'/operatorpos'
'/operatorsub'
'/operatortruediv'
'/operatortruth'
'/operatorxor'
'/math/random'
'/randomrandom'
'/randomsystemrandom'
'/randombetavariate'
'/randomchoice'
'/randomexpovariate'
'/randomgammavariate'
'/randomgauss'
'/randomgetrandbits'
'/randomgetstate'
'/randomlognormvariate'
'/randomnormalvariate'
'/randomparetovariate'
'/randomrandint'
'/randomrandom-1'
'/randomrandrange'
'/randomsample'
'/randomseed'
'/randomsetstate'
'/randomshuffle'
'/randomtriangular'
'/randomvonmisesvariate'
'/randomweibullvariate'
'/requests'
'/requestsconnectionerror'
'/requestscerts'
'/requestshttperror'
'/requestshead'
'/requestsget'
'/requestsgetheaders'
'/requestsgettext'
'/requestsgetparameters'
'/requestsrequest-1'
'/requestsstatus_codes'
'/set'
'/setadd'
'/setclear'
'/setdifference'
'/setdifference_update'
'/setintersection_update'
'/setisdisjoint'
'/setdiscard'
'/setintersection'
'/setissuperset'
'/setissubset'
'/setpop'
'/setremove'
'/setsymmetric_difference'
'/setsymmetric_difference_update'
'/setunion'
'/setupdate'
'/sqlite3'
'/sqlite3connect'
'/string'
'/strcapitalize'
'/strcasefold'
'/strcenter'
'/strcount'
'/strencode'
'/strendswith'
'/strexpandtabs'
'/strfind'
'/strformat'
'/strformat_map'
'/strindex'
'/strisalnum'
'/strisalpha'
'/strisdecimal'
'/strisdigit'
'/strisidentifier'
'/strlower'
'/strisnumeric'
'/strisprintable'
'/strisspace'
'/stristitle'
'/strisupper'
'/strljust'
'/strjoin'
'/strlstrip'
'/strmaketrans'
'/strsplit'
'/strpartition'
'/strpartition'
'/strsplitlines'
'/strreplace'
'/strrfind'
'/strrstrip'
'/strrindex'
'/strrjust'
'/strrpartition'
'/strrsplit'
'/strswapcase'
'/strstartswith'
'/strtranslate'
'/strupper'
'/strzfill'
'/string-constants'
'/stringascii_letters'
'/stringascii_lowercase'
'/stringascii_uppercase'
'/stringchainmap'
'/stringdigits'
'/stringhexdigits'
'/stringtemplate'
'/stringoctdigits'
'/stringprintable'
'/stringwhitespace'
'/time'
'/timealtzone'
'/timeasctime'
'/timeclock'
'/timectime'
'/timedaylight'
'/timeget_clock_info'
'/timegmtime'
'/timelocaltime'
'/timemonotonic'
'/timeperf_counter'
'/timeprocess_time'
'/timesleep'
'/timestrftime'
'/timestruct_time'
'/timetime'
'/timetimezone'
'/timetzname'
'/tuple'
'/tuplecount'
'/tupleindex'
'/tkinter'
'/tkinterarc'
'/tkinterboth'
'/tkinterbottom'
'/tkinterbutton'
'/tkinterbuttontext'
'/tkinterbuttonpack'
'/tkinterbuttonwidth'
'/tkinterbuttongrid'
'/tkinterbuttongridcolumn'
'/tkinterbuttongridcolumnspan'
'/tkinterbuttoncommand'
'/tkinterbuttongridrow'
'/tkinterbuttonfg'
'/tkinterbuttonpack'
'/tkinterbuttondestroy'
'/tkintercanvas'
'/tkintercanvascreate_line'
'/tkintercanvasheight'
'/tkintercanvaswidth'
'/tkintercanvasbind'
'/tkintercanvascreate_arc'
'/tkintercanvascreate_oval'
'/tkintercanvascreate_ovalsfill'
'/canvaspack'
'/tkintercheckbutton-1'
'/tkintercheckbuttongrid'
'/tkintercheckbuttonpack'
'/tkintere'
'/tkinterentry'
'/tkinterentrydelete'
'/tkinterentrydestroy'
'/tkinterentrygrid'
'/tkinterentryget'
'/tkinterentrypack'
'/tkinterentryget'
'/tkinterentryset'
'/tkinterentrytextvariable'
'/tkinterentrygridcolumnspan'
'/tkinterframe'
'/tkinterframepack'
'/tkinterframewidth'
'/tkinterframeheight'
'/tkinterframebackground'
'/tkinterframebind'
'/tkinterframepack'
'/tkinterframegrid'
'/tkinterleft'
'/tkinterlistbox'
'/tkinterlabel'
'/tkinterlabelbackground'
'/tkinterlabelfg'
'/tkinterlabeltext'
'/tkinterlabelpack'
'/tkinterlabelgrid'
'/tkinterlabelframe'
'/tkinterlabelframetext'
'/tkinterlabelframefg'
'/tkinterlabelframefont'
'/tkinterlabelframepack'
'/tkinterlistbox'
'/tkinterlistboxbackground'
'/tkinterlistboxfg'
'/tkinterlistboxselectbackground'
'/tkinterlistboxhighlightcolor'
'/tkinterlistboxgrid'
'/tkinterlistboxinsert'
'/tkintermenubutton'
'/tkintermenu'
'/tkintermenuadd_command'
'/tkintermessage'
'/tkintermessagepack'
'/tkintermessagegrid'
'/tkintermessagebox'
'/tkintermessageboxshowinfo'
'/tkintern'
'/tkinterne'
'/tkinternw'
'/tkinteroptionmenu'
'/tkinteroptionmenupack'
'/tkinteroptionmenuvariableset'
'/tkinterright'
'/tkinterradiobutton'
'/tkinters'
'/tkinterstringvar'
'/tkinterspinbox'
'/tkinterspinboxfg'
'/tkinterspinboxfrom_'
'/tkinterspinboxto'
'/tkinterspinboxvalues'
'/tkinterspinboxpack'
'/tkinterstringvar'
'/tkinterse'
'/tkintersw'
'/tkintertext'
'/tkintertextpack'
'/tkintertk'
'/tkintertkbind'
'/tkintertkbindbuttonpress'
'/tkintertkconfig'
'/tkintertkdestroy'
'/tkintertkmainloop'
'/tkintertktitle'
'/tkintertkgeometry'
'/tkintertop'
'/tkinterw'
'/webbrowser'
'/webbrowsererror'
'/webbrowserget'
'/webbrowserget'
'/webbrowseropen'
'/webbrowseropen_new'
'/webbrowseropen_new_tab'
'/webbrowserregister'
'/turtle'
'/turtleback'
'/turtlebackward'
'/turtlebegin_fill'
'/turtlebegin_poly'
'/turtlebgcolor'
'/turtlebgpic'
'/turtlebk'
'/turtlebye'
'/turtlecircle'
'/turtleclear'
'/turtleclearscreen'
'/turtleclearstamp'
'/turtleclone'
'/turtlecolor'
'/turtlecolormode'
'/turtledegrees'
'/turtledelay'
'/turtledistance'
'/turtledone'
'/turtledot'
'/turtledown'
'/turtleend_fill'
'/turtleend_poly'
'/turtleexitonclick'
'/turtlefd'
'/turtlefillcolor'
'/turtlefillcolor'
'/turtlefilling'
'/turtleforward'
'/turtleget_poly'
'/turtlegetcanvas'
'/turtlegetpen'
'/turtlegetscreen'
'/turtlegetshapes'
'/turtlegetturtle'
'/turtlegoto'
'/turtleheading'
'/turtlehideturtle'
'/turtlehome'
'/turtleht'
'/turtleisdown'
'/turtleisvisible'
'/turtleleft'
'/turtlelisten'
'/turtlelt'
'/turtlemainloop'
'/turtlemode'
'/turtlenuminput'
'/turtleonclick'
'/turtleondrag'
'/turtleonkey'
'/turtleonrelease'
'/turtleonscreenclick'
'/turtleontimer'
'/turtlepd'
'/turtlepen-1'
'/turtlepencolor'
'/turtlepensize'
'/turtlepenup'
'/operatorpos'
'/turtleposition'
'/turtlepu'
'/turtleradians'
'/turtleregister_shape'
'/turtlereset'
'/turtleresetscreen'
'/turtleresizemode'
'/turtleright'
'/turtlert'
'/turtlescreensize'
'/turtleseth'
'/turtlesetheading'
'/turtlesetpos'
'/turtlesetworldcoordinates'
'/turtlesetx'
'/turtlesety'
'/turtleshape-1'
'/turtleshapetransform'
'/turtleshearfactor'
'/turtleshowturtle'
'/turtlespeed'
'/turtlest'
'/turtlestamp'
'/turtletextinput'
'/turtletilt'
'/turtletitle'
'/turtletiltangle'
'/turtletowards'
'/turtletracer'
'/turtleturtles'
'/turtleturtlesize'
'/turtleundo'
'/turtleundobufferentries'
'/turtleup'
'/turtleupdate'
'/turtlewidth'
'/turtlewindow_width'
'/turtlewindow_height'
'/turtlewrite'
'/turtlewrite_docstringdict'
'/turtleycor'
'/turtlexcor'
'/system'
'/system/errors'
'/Contactus'
'/sitemap.xml'
'/requestsstatus_codes'
'http://www.pythonlake.com/aboutus'
'http://www.pythonlake.com/Contactus'
'http://www.pythonlake.com/appointment'
'http://www.pythonlake.com/download'
'http://www.pythonlake.com/operators'
'http://www.pythonlake.com/keywords'
'http://www.pythonlake.com/built-in'
'http://www.pythonlake.com/boolean'
'http://www.pythonlake.com/dictionary'
'http://www.pythonlake.com/math'
'http://www.pythonlake.com/integer'
'http://www.pythonlake.com/decimal'
'http://www.pythonlake.com/math/random'
'http://www.pythonlake.com/requests'
'http://www.pythonlake.com/set'
'http://www.pythonlake.com/frozenset'
'http://www.pythonlake.com/list'
'http://www.pythonlake.com/tuple'
'http://www.pythonlake.com/fractions'
'http://www.pythonlake.com/fraction'
'http://www.pythonlake.com/string'
'http://www.pythonlake.com/string-constants'
'http://www.pythonlake.com/control-flow'
'http://www.pythonlake.com/itertools'
'http://www.pythonlake.com/time'
'http://www.pythonlake.com/tkinter'
'http://www.pythonlake.com/turtle'
'http://www.pythonlake.com/system/app/pages/sitemap/hierarchy'
'https://www.google.com/a/UniversalLogin?service=jotspot&continue=http://sites.google.com/a/pythonlake.com/www/contactus'



The for loop returns the headers values.
 >>> import requests
>>> r=requests.get('http://www.pythonlake.com/aboutus')
>>> for s in r.headers:
...     print(s)
... 
x-xss-protection
transfer-encoding
x-content-type-options
vary
expires
content-type
server
date
cache-control
accept-ranges
last-modified
x-robots-tag
alternate-protocol


The headers returns with the server response to get request and if statement is used to open the webbrowser condition to dict.keys.
 >>>import requests
>>> re=requests.get('http://www.pythonlake.com', params='Pythonlake')
>>> re.headers
{'x-content-type-options': 'nosniff', 'server': 'GSE', 'x-robots-tag': 'noarchive', 'x-xss-protection': '1; mode=block', 'date': 'Mon, 27 Apr 2015 13:27:21 GMT', 'accept-ranges': 'none', 'vary': 'Accept-Encoding', 'cache-control': 'public, max-age=5', 'transfer-encoding': 'chunked', 'content-type': 'text/html; charset=utf-8', 'last-modified': 'Mon, 27 Apr 2015 13:16:45 GMT', 'expires': 'Mon, 27 Apr 2015 13:27:26 GMT', 'alternate-protocol': '80:quic,p=1,80:quic,p=1'}
>>> 
>>> h.keys
<bound method CaseInsensitiveDict.keys of {'x-content-type-options': 'nosniff', 'server': 'GSE', 'x-robots-tag': 'noarchive', 'x-xss-protection': '1; mode=block', 'date': 'Mon, 27 Apr 2015 13:27:21 GMT', 'accept-ranges': 'none', 'vary': 'Accept-Encoding', 'cache-control': 'public, max-age=5', 'transfer-encoding': 'chunked', 'content-type': 'text/html; charset=utf-8', 'last-modified': 'Mon, 27 Apr 2015 13:16:45 GMT', 'expires': 'Mon, 27 Apr 2015 13:27:26 GMT', 'alternate-protocol': '80:quic,p=1,80:quic,p=1'}>
>>> if 'server' in h.keys():
...     >>> webbrowser.open_new('www.pythonlake.com')
True
>>>


The request request is subject to the exception of ConnectionError in finding the turtle parameters in the www.pythonlake.com homepage.
>>> import requests
 >>> try:
...     r=requests.get('http://www.pythonlake.com',params='www.pythonlake.com')
...     t=r.text
...     t.find('turtle')
... except:
...     print(e)
...     
103986
>>> 


The code gets http library of  www.pythonlake.com and store in the variable r and str.count method is used to count the strings.
>>> import requests
>>> r=requests.get('http://www.pythonlake.com')
>>> t=r.text
>>> t.count('www.pythonlake.com')
721 

If statement is used on the method of requests.request() to get the open link in the webbrowser.
 >>> import requests
>>> re=requests.request('Get','http://www.pythonlake.com')
>>> re
<Response [200]>
>>> if re.ok==True:
...     webbrowser.open_new('www.adilwaseem.com')
...     
True
>>> 

Subpages (36): View All
Comments