Inthisvideo, we'regonnabegoingover a veryquickoverviewofboththebeautifulsoupmodulepythonaswellastherequestsmoduleinpythonaswell.
Soifyou'renotfamiliar, a verybriefintroductiontobothofthoserequestsisgoingtobe a modelthat's goingtoallowustoaccessVariousresourceisontheweb, soallowustoobtaininformationfromvariouswebsitesnavigatetothosewebsites, andbeautifulsoupisgoingtoallowustoparsethatinformation.
Sowhatwe'regonnadonextiswe'regoingtoimportthemandusethem, sowe'regonnaimportrequests, andthenwe'regoingtoimport a specificclassforthe B s formodulethatweinstalledspecificallythebeautifulsoupclass.
Sothisisgoingtoallowustointhetermsofbeautifulsoupsoupif I contentthatweobtainedfrom a websiteandagainthatwillmakeitpossiblesowecanextractinformationfromthatcontent.
Okay, sothefirstthingwe'regonnadoiswe'regoingtousetherequestmodule, and I guessbefore I shouldgoforbeforewegoanyfurther, I wanttomentionthatallofthecode, alongwiththecommentsthatiskindofsupplementingwhat I'm sayinghere, allthatisgoingtobeprovided.
Um, I gethubandlinkedtothatwillbeaccessibleinthedescriptionsoyoucouldgoaheadanddownloadthat.
Ifyouwantsomeadditionalinformationastowhat I'm doingorsayingrightsobacktothis, we'regoingtocreate a variablecalledresultsandwe'regoingtosayrequestdotgetAndtheyweregoingtopassinthewebsitethatthatwewanttoaccess.
So I'm justgoingtomovemybrowseroverhere, so I'm justgoingtoaccessthispagerightthere, So I'm justgonnagoaheadandgetthatAndthenwhatwe'regoingtodotomakesurethatthepagewasactuallyaccessiswe'regoingtoprintoutthestatuscode, whichisjust a httpstatuscodelettingusknowwhetherornotthepagewasaccessibleornot.
Anotheronethatithasisdotcontentandthatisgoingtoreturnthesourceofthatpageandwhatwe'regonnado, storethatinto a verywellcalledSRCforsource, andthenwe'regoingtohavethisvariable.
Solet's goaheadandrunthat, and I'm justgonnaprintoutthesourcesoyoucouldactuallyseewhatitlookslikegonnarunthatlikethis.
Sowesee a bunchofoutputhere, andthisisreallyjusttheWebsourceofthegoogledotcomhomepage, so I'm justgonnagoahead, deletethat.
So, forinstance, onethingthatwecoulddonowthatwehavethesoupobject, iswecoulddothingslikegivemeallofthelinksonthepage.
Sowhatwecoulddoiscreate a variablewhich I calllinks, and I cansaysoup, notfindallsofinalis a methodprovidedfromthisbeautifulsoupobjectthatwereaccessinghere.
Andwhatdo I wanttofindallofonthispage?
I wannafindalloftheeighttags.
Sothis a istheargumentthat's beingpassedandittellsbeautifulsouptosay, Hey, findalloftheeighttagsareallofthelinksonthispageandthenstoretheminthisvariablelinks, andthenwe'regonnagoaheadandactuallyjustprintthoseouttothescreen.
So I'm saying, LookatthetextinthatLincolnthatlistandisthewordaboutinthatinthattext?
Ifso, thengoaheadandprintoutthatlatebecause I careaboutthatlinkandactuallygoaheadandprintoutthe, um, printsouttheactualthingthatitgoesto a CZ.
Well, sothat's whatthisisdoinghere.
SoLinkalsohas a, uh, a tribute.
Or I shouldsay, if a function a TTrsand I'm lookingfor a specificattributesofthatlink.
Soinsidethe a tag, there's attributesinsideofthat, whichisthe H r E f taginsideofthat, and I'vesaidgivemethecontentofthat H R E f insideofthelinktag, whichistheinsideofthistagthathasthe A.
Soit's kindof a lotofRussiadullkindofstructuregoingoutthere.
So, forinstance, weseethiswholeThisisthewhole A tag.
Sowehaveeverythingfromthestartofthe A tagtotheendofit, includingthetext, includingthe H Raph.
Andthenwhatwedidwiththenextlineiswesaid, Hey, insideofthe a tagthere's thisattributescalled a treffandwewanttoaccessthecontentoftheattributesinsideofthe A tagandwhatwedidthere.
That's preciselythis.
Andthat's whatwe'reprintsitoutrighthere.
So I hopethatmakessense.
I knowthat's kindofdrillingdownquite a bit, andyoucouldgoprettycomplicatedwiththesetypesofexpressions, but I thinkonceyouunderstandthislevel, youcouldarbitrarilyapplydifferentlevelsofcomplexitytoyourownsituations.
See, youcouldseeas I moveoverthiscontent, variousthingsarebeinghighlightedtoindicatethatthesethingscorrespondtothecodethat I'm sortofmousingoverhere.
It's a verynicetool.
Sobasicallyonethingthatwecouldkindofobserveisthatalloftheselegsif I clickonthisoneaswellalloftheselinksarecontainedinthese H twoheadyclasstagsSoinsideofthe H twotagswehavethethe a linkorthelinkthatwe'reactuallyafterSoyoucanseethatallofthelinksonthispageareinfactfourbeddedinthisway.
Soallofthesestatesareinbetween h twotagsandonethingthatwecoulddo.
Let's sayfour h totagIDsoupdotfiedunderscoreallandthenhere.
Remember, ifwegobacktothisinitialfilehere, wewantedtofindallthelinks, andwhatwedidiswesaid, findallthethingsthathadthe a tag.
Well, inthiscase, wedon't wanttofindallthethingswith a tagjustyet.
Wewanttofindallthethingswiththe H twotag, sowe'llgoaheadandsay, Findallofthewoodsinthissoupthathavethetag h twoandtheninsideofthatIfyourecallforgobacktothatpageinsideofthe H twotag, there's an a tag, andthat's whatwewanttoextract.
Sowe'llgoaheadandminimizethisandsay, Okay, nowthatwe'reloopingthroughold H twotags, let's say a tagisequaltoeachtotag, notfind.
Andwhatdowewanttofind?
Wewanttofindthe A taginsideofthat.
Sothere's findallandthere's findsofindallisgoingtobe a functionthat's goingtoreturntous a list.
It's goingtoreturnalloftheonesinthepage.
Theremightbenoitemsofthisformonthepage.
Theremightbeone.
TheremightbeMandy.
Inanycase, it's goingtoreturntous a list.
This a tagisgoingtojustfind a singleelement.
Soit's justgoingtofind a tag.
Thefirsttagthatcorrespondstothisandthenwe'regoingtostorethatinthisvariable a taghereNowwewanttodoWehavehypotheticallyare a tag.
We'regoingtoaddthattoe.
Areyouearl's listthatwehaveuphere?
Sowe'regonnasayyouwhereelsedotdependandtheyweregoingtosay a a tag.
Andactually, I don't wanttojustaddthe a tag I actuallywanttodo, kindofsimilartowhatwedidoverherewhere I saidnotjusttheleg, but I wanttheattributesofthatlinkthatcorrespondsto a traf.
Sogoingbacktothisstatehere, we'velivedthroughallofthe H twotagsonthepageinsideoftheloop, wehavethisthingthatsaysOKinsidethat h totagthereshouldbeat a tag.
Andactually, furthermore, I wantthe h refattributes.
I wanttheactuallegthatisinsideofthis a trapattributes.
Solet's goaheadandadd.
Doyoustillbeenverysimilar?
Solet's say a tagdot a t TRSandthenwe'lldo a troughjustlikewedidbefore.
Okay, sothatisprettymuchallweneedthereandthenjusttomakesurethatweactuallyhave a listthat's populatedproperly.
Soagain, assumingthatyouhavethisinstalled, we'rejustgonnasayfrom B isforimportbeautifulsoup, whichistheclassthatwe'regonnausetosuperfi, theHTMLconceptthatwe'regoingtoeventuallyparsonthisvideoandtheissueofcontentthatwe'regoingtoParson, thisvideoisactuallyjustgoingtobedescribedbythisvariablehere, whichis a stringthatreallyjustcontains a very, verysimpleHTMLWebpage.
However, ifthesourcecodeforthatwebsiteischangedanyway, thatthevideomaybecomingcorrectas a resultbecausethesourcechangeandthereforeoursourcemightnotbedoingthecorrectthing.
Ah, andatthetime, wheneveryouhappentobewatching, thiscouldbegivingyou a differentresultthatwhatyouactuallyexpect.
Sothisstringisjustgoingtodescribe a verysimpleHTMLWebpagethatwe'regoingtouseourexampleandjusttogiveyou a senseofwhatthislookslike.
We'regoingtojustgotogoaheadandwritethiscontentto a file.
Sowhat I'm doinghereis I'm justcreating a filecalledIndexhtmlandthenwritingthatcontentthatwedefinedofabove.
SotheHTMLDocString, I'm justgonnagoaheadandwritethatoutto a file.
Sothenextthingthatwe'regonnadoiswe'regoingtocreate a superobjectbasedontheHTMLdocstringvariablethatwedefinedupabove.
AndsothisisgoingtoallowustoparsetheHTMLcontent.
Sowhatdid I do?
Wanttomentionthat I don't believe I didmentioninthequickstartguideisonethingthat's kindofneatisyoucanusethisprettifyfunctionwhichhadprovidedfromthebeautifulsoupclass, whichallowsyoutooutputtheHTMLcodeinsortof a nicelyformattedway.
So I'm justgonnagoaheadandwritethisandgivethis a run.
Sowe'regonnakindofbuildupto a waythatwecanfindallofthetagsof a givenelement.
Soanotherthingthatwecandoitthat's verysimilartowhatwejustdidinsteadofsoupdot b orsoupedupthat p wecoulddo a similarthingwhereweusedthefindfunction.
Soifwesaysoup, notfindandthengiveitthetagthatwe'relookingfor, thiswillgiveusagainthefirstoccurrenceintheHTMLdocumentationstartingtheHBOdocthathas a boldtax.
Soifyougoaheadandwritethisand I'm justgonnacleartheterminalsowedon't havetoomuchoutput, weseethatthisisalsothefirstoccurrenceofthebowltackthatwegotfrombeforewhatwejustdidsoupedup B.
So I'm gonnagoaheadanduncommonThisandthefunctionthatwe'regoingtomakinguseofhereisfindunderscoreall.
Sothisis a builtinfunctionthatwecanuseof a soupobjectthatisgoingtofindallofthecontentinthissoupthatpertainstothisbowltagsowecouldgoaheadandwritethisandrunit, andwe'llseethatthisreturns a listwhereeachoftheelementsinthislistarebeautifulsoupobjectsthatcorrespondtoeverythingthatcontains a boldtag.
Sothat's alsogoodtoknow.
Solet's keepmovingdowninthisfile.
Soanotherthingthatwecanalsothinkaboutissomethingcalled a name.
So, forinstance, ifwewantedto, forwhateverreason, alterthenameofthistag, wecoulddosointhefollowingwaysowecandefine a variabletagwhichisequaltosoupdotBeSothisisthefirstoccurrenceof a bolditemintheHTMLdocumentthatwe'reparsing.
Butifwewentbacktothisfunctionherewherewefoundallofthebowltags, rememberthatreturntous a listofallofthebeautifulsoupobjectsthatcorrespondedto a bulltaginourHTMLdocument.
So I'm essentiallydoinghereis I'm sayingfindallofthoseelementsandjustgivemetheelementindextoofthatlist.
Andthenlet's justprintthatout.
Justseewhatwegotsowe'llgoaheadanddothat.
Sointhiscase, it's a boldtagthathasideaswithoneandtheninbetween, thatis, thisisthisnameTestone, andthenit's theendofthebulltag.
Solet's goaheadandworkwiththisandseewhatwecando.
Sothisspecifictagswesawhasattributes, soit's a bulltag, butithasanattributeinsideofit, whichiscalled I D.
Soit's overloadedthatifwecanaccessthe i d fieldofthisbysayingtagopensquarebracket i d.
Closesquarebracket, whichisthenameoftheattributesthatwewanttoaccesstogoaheadandwritethatwecanseethatthevalueofthe I d fieldthatwejustaccessesabletowant.
Whatifwejustwantedtoseealloftheattributesthisparticulartaghadsobeautifulsuitprovidestoussomethingcalledinthismethodhere, a T T E R s, whichisgoingtoallowustoseealloftheattributesof a giventag.
Soifwegoahead, writethisin, printitout, weseethatitreturns a dictionarywherethekeyisthenameoftheattributes.
Andthenthevalueisthevalueoftheoftheattributes.
Sothekeyhere, anotherattributeThisisequaltoonethat's thevalueofthecenturyinthedictionaryandlikewisethiskeyherefor I d.
Soif I dosomethingliketaggedoutstringandthenreplacewithsothisisjust a functionthatisgoingtoallowustoreplacethecontentthatisthatstringwithsomethingelseand I replacedthecontentwiththisisanotherstring, andthenifweprintouttheresultsofthat, we'llseewe'llseewhatweget.
Soletmejustkindofreviewwhat I'm doinghere.
Creatingthevariabletagthatwe'veseenbeforeprintingouttheoriginaltagwithnoalterationswhatsoeverthat I'm usingthisdotreplacedwithMethodtoeactuallyalterthetextinsideofthatbeautifulcityofobject, and I'm goingtoprintouttheresultingtag.