First Web Scraper

By • CCCB Lab in Barcelona

Recording

Show the timestamped transcript
  1. Hey guys, welcome back, thanks for staying, thanks for sticking in.
  2. So before we get going, what I had planned, and we can always freewheel and freelance
  3. and switch it around, is a pretty serious coding man.
  4. We're gonna dig in, we're gonna write some code.
  5. There's a whole tutorial I have planned for us to work through where we're gonna do something
  6. called web scraping.
  7. But before we get going, I just kind of want to assess what kind of experience people have.
  8. The lesson is put together so it assumes absolutely zero experience, but I want to see kind of
  9. what's going on.
  10. So if I could just ask a question and people raise their hands, that would be awesome.
  11. So who here has done programming on the command line or the terminal of their computer before?
  12. Anyone?
  13. It's okay, it's totally okay.
  14. So who here has ever written code in the Python programming language?
  15. Okay a couple.
  16. And who here has Python installed on their computer?
  17. Okay a couple.
  18. So we're gonna have to install Python, I think it's part of the class.
  19. And so then ultimately, has anyone here ever written a web scraper before or even know
  20. what web scraping is?
  21. No.
  22. Okay so you know what it is?
  23. And keep your hand up if you've done it.
  24. How'd you do it?
  25. Scrapes in the middle.
  26. It's a button?
  27. It's a button?
  28. That's awesome.
  29. Yeah so basically if you guys don't know, it sounds like most of you do, web scraping
  30. are different techniques that basically suck the data out of a website where it's kind
  31. of encoded with all the other crap that's in the page and just drops it into something
  32. like a spreadsheet so you can access it like any other data.
  33. And there's a lot of different ways to do it and they're getting increasingly automated.
  34. But since every webpage is different and things are put up in different ways, it's often
  35. the case that you need to write code yourself as opposed to rely on some of these tools
  36. which are great and for simple stuff will work pretty well I bet.
  37. But for more complex websites that are just weird government stuff, probably can't do
  38. the job.
  39. So the goal of the class, which I'm calling a first web scraper, is for everybody here
  40. to one, get their feet wet and do a little command line mucking.
  41. Get Python going, do a little Python mucking, learn some of the basics of Python which you
  42. can use to all kinds of stuff.
  43. And then if we get there, write a little web scraper.
  44. And it won't be much more impressive than what the button can do but it'll teach you
  45. the fundamentals of how you would write one yourself.
  46. Does this sound like a reasonable proposal, does everybody think this is a good idea?
  47. But before we start, this is going to be painful.
  48. So in English we have a saying called a crash course, do you guys say this in Spanish?
  49. So a crash course is sort of like a class where things are moving really too fast and
  50. kind of out of control and a lot of stuff breaks.
  51. So there's no way you're ever going to master programming in two hours or even get all
  52. that far but we're going to try to get a lot of the basics in and a lot of stuff's going
  53. to go wrong and I'm going to run around the room and help people when they get stuck and
  54. all I ask is that you hang with me, okay?
  55. Sound good?
  56. All right.
  57. So I'm going to fire up my computer here and I'm going to give you the URL of a website
  58. where I have the entire class scripted out.
  59. So if you think I'm going too slow or too fast, you can move at your own speed right
  60. there, okay?
  61. So here's my text editor.
  62. Let's bump up the font size on this bad boy.
  63. Oh, by the way, is everybody, we have a mix of Windows and Mac, is that right?
  64. Okay, that's what I figured.
  65. That's fine.
  66. So we're going to have two different sets of bugs to work through, guys.
  67. You're not as excited as I am.
  68. Okay.
  69. Select.
  70. Let's see if this gets big now.
  71. Yeah.
  72. Yes.
  73. Oh, it's not up there.
  74. Let's see if I can, like, mirror the screen so I'm seeing the same thing you're seeing.
  75. Hey, how about that?
  76. Hola.
  77. Right?
  78. Okay.
  79. Is that big enough?
  80. Can everybody read that?
  81. Okay.
  82. Great.
  83. So what I'm going to do is I'm going to kind of write stuff up here and as we go and you
  84. guys can follow along on your computers.
  85. If at any point you have a question, just shout it, okay?
  86. And since I need to type, I might put down the mic and just yell for a little bit if
  87. that's okay.
  88. If that's not a big deal to anyone, okay.
  89. I'll move this mic up.
  90. Well, no.
  91. Whatever.
  92. Okay.
  93. Can you hear me?
  94. I'm coming.
  95. I'm coming.
  96. I need both hands.
  97. Okay.
  98. So the website where we're going to go, oh, thank you, I'm going to open a web browser.
  99. I'm using Linux, guys.
  100. Yeah.
  101. Anybody else?
  102. No.
  103. Get with it.
  104. Okay.
  105. So the website that has the documentation, we're going to walk through.
  106. Where's my mouse here, guys?
  107. Did it get lost on some other screen?
  108. Am I really mirrored?
  109. Look who's having the technical difficulties.
  110. Come on.
  111. Come on.
  112. Yeah.
  113. Where's my mouse?
  114. Okay.
  115. So the website you're going to go to while I figure that out is firstwebscraper.rtfd.org.
  116. You guys see that?
  117. So open that up in your web browser.
  118. RTFD, anyone know what that stands for?
  119. It's a nerd acronym in computer programming, which stands for read the fucking documentation.
  120. And it's a website that lets you host documentation for free.
  121. And so that's why we're using it.
  122. And you should land here.
  123. Everybody got it?
  124. Okay.
  125. And so this is the really long thing I've written that has all the stuff, including
  126. every command we're going to run.
  127. Oh, wait.
  128. I went to first news app.
  129. I went to the wrong site, didn't I?
  130. It should be firstwebscraper.
  131. That's a different class, guys.
  132. We're not doing that one.
  133. This is what you see, right?
  134. Not news app, web scraper.
  135. Great.
  136. And so this is everything we're going to do, all the commands, and a lot of the stuff
  137. I'm going to say.
  138. But we're going to go through it a lot slower, and we're going to kind of take our time.
  139. But if you get bored, you can move ahead.
  140. Or if we move too fast for you, you can hang behind.
  141. And our ultimate goal at the end of this, if we get there, is to scrape the data from
  142. the roster of jail inmates at the Boone County Jail in Boone County, Missouri, guys, who's
  143. been to Boone County, Missouri.
  144. Yeah.
  145. This is the University of Missouri, where I graduated.
  146. And this little dumpy website you can click on includes who's in lockup tonight.
  147. And so we're going to bust some data out of the Boone County Jail, guys.
  148. Jailbreak.
  149. Who's excited?
  150. No?
  151. Just me?
  152. Okay.
  153. But before we do all that, we got to get all our tools in order.
  154. And these are the basic tools that you're going to use your entire career as a computer
  155. programmer, or at least until Oculus Rift replaces, you know, the keyboard or whatever.
  156. And they're going to be necessary before we can even get started.
  157. And so we got a lot of boring fundamentals we got to get out of the way that you got
  158. to have in.
  159. And you can see that in this first section here called prerequisites.
  160. And the first thing that's required is a command line interface to interact with your computer.
  161. And so what's the command line interface?
  162. It's the goofy thing that looks like this, right, that you can type into, who am I?
  163. My favorite existential command.
  164. And it'll return for you a lot of different stuff about the computer, right?
  165. This is just like the hackers in the movies, right?
  166. That's what we need to be issuing commands directly to the computer and have it respond.
  167. Like say, run my Python script, right?
  168. That sort of thing.
  169. And so every operating system has one of these.
  170. They're just slightly different.
  171. And you can install replacements that are a little snazier like the one I'm using.
  172. But let's just now make sure everybody can open their command line interface.
  173. And no one's done it before.
  174. So this will be your first.
  175. Oh, this is awesome.
  176. Okay.
  177. So if you're a Mac user, right, you got that thing spotlight or the little search thing
  178. up in the corner.
  179. You know what I'm talking about?
  180. Okay.
  181. In the upper right-hand corner, in the upper right-hand corner, I don't have a Mac here,
  182. but in the upper right-hand corner, there's a little Sherlock.
  183. I appreciate it.
  184. Thank you.
  185. And you got to open that, and you're just going to type in the word terminal.
  186. Terminal.
  187. I'm causing trouble.
  188. And so that's going to then open, guess what, the terminal, okay?
  189. And you can go ahead and do just like I did, and type in, type in the existential command,
  190. who am I, right?
  191. And hit enter, and you're going to see it's going to return the name of your user, right?
  192. Now if you're in Windows, that might not work, because I don't know if Windows is that existential
  193. man.
  194. It doesn't look inward.
  195. It works excellent.
  196. Okay.
  197. I take it back.
  198. But now if you're in Windows, there's a slightly different thing you have to do.
  199. You have a start menu.
  200. I bet in the lower left-hand corner, right?
  201. And so if you hit your start menu, and this may vary depending on your version of Windows,
  202. there's a little box in there that you can type stuff into, right, and it'll pull up
  203. programs.
  204. Try typing in there three letters, C, M, D. And the Windows people can tell me if I'm
  205. getting this wrong, but you just type C, M, D into that little thing, and it should open
  206. a black DOS terminal, right?
  207. The one that made Bill Gates so rich.
  208. And so that is basically, that's the Windows version of the command line interface.
  209. And unfortunately, DOS and the Mac system, which is based on Unix, which is the same
  210. thing as Linux, have two slightly different ways of doing everything.
  211. They're very close, but we're going to have differences in what they can do.
  212. And so I'm hoping that now we can just have a brief segment where we walk through a lot
  213. of the basic commands that you will use every day once you get used to doing this.
  214. And if you become a programmer, you will do this every day, all day, that allow us to
  215. create files and directories, move things around, inspect the file system, and execute
  216. files, okay?
  217. And so that's going to be the first chapter of the documentation on the website that's
  218. called Act 1 command line.
  219. We're going to go through that now, okay?
  220. Everybody has their terminal up?
  221. We all got there?
  222. We're doing great.
  223. Boom County better watch out.
  224. All right.
  225. So now the teacher will remind himself.
  226. All right.
  227. So the first command we're going to learn is one that will just print the current directory
  228. then you're in.
  229. Not instead of who am I, where am I, all right?
  230. And there's two slightly different ways to do this.
  231. If you're in a Linux system, you'll simply type the letters P, W, D, and hit enter.
  232. Should I size this up too?
  233. Yeah.
  234. Bigger.
  235. Right?
  236. And if you're in Windows, just hit CD and push enter.
  237. So try doing that.
  238. And you should see it print something back out at you, right?
  239. And the way the command line works is when you issue a command that the computer likes
  240. and it knows how to use, it does the no bells go off.
  241. It doesn't reward you.
  242. It's not like there's any magic thing that happens.
  243. It just sort of quietly finishes its command and prints out the result of what it wanted.
  244. So you did P, W, D, right?
  245. And it printed out a little line there or CD and it printed out a little line.
  246. That line is the location where you currently are on your computer's file system, right?
  247. And that is going to be the home directory for your user.
  248. And I bet if you've ever used my computer or the finder or whatever they call it on
  249. Apple to move around, you kind of recognize that user name and where some of the stuff
  250. is, right?
  251. Because you're now browsing and sitting inside of the same file system and folder structure
  252. that you see when you navigate the folders via the user interface or via the finder and
  253. the my computer, right?
  254. You're inside of it operating within that same playing field.
  255. And this is your current location, which you can move around it.
  256. And so P, W, D, which stands for, anyone want to guess?
  257. It's pretty terrible.
  258. Present working directory, right?
  259. And CD actually stands for change directory, except you're not changing directories.
  260. It's just telling you the current directory, which doesn't really make sense either, right?
  261. And you may say, why do these things have such dumb short names?
  262. And why don't they really make sense?
  263. And the reason is, is because they were just invented by nerds in like the late 70s and
  264. early 80s who were coming up with all this stuff and nobody came up with anything better.
  265. And they caught on way more than any of them ever thought they would.
  266. And now we have to live with them because the entire world depends on how they operate.
  267. Right?
  268. And so what you'll find as you get inside the internals of your computer and you start
  269. to do what programmers do and live in programming land, you'll see particularly on the open
  270. source side of the spectrum, you'll see a lot of it is just kind of haphazard and has
  271. odd names.
  272. And it's sort of weirdly just the way it is.
  273. And you just kind of have to learn the way it is and memorize a bunch of little stuff
  274. and you'll kind of get it.
  275. And it sucks, but that's the way it is.
  276. And it's all the result of some guy, probably Dennis Ritchie in 1977 at Bell Labs in New
  277. Jersey said, I'm going to call it PWD or whatever, whoever it was, right?
  278. And so now it's still PWD.
  279. There's no better reason than that.
  280. Don't feel stupid because you don't know that stuff.
  281. It's just some dumb decision, some nerd made, okay?
  282. And as you write code yourself, you'll see how hard it is the name thinks.
  283. And they just keep it short because they didn't want to type so much.
  284. You know what I mean?
  285. It's a pain in the butt to type present working directory, okay?
  286. So let's see.
  287. The next thing we're going to learn is how do we find out what's inside this directory?
  288. Can I take a look at it and list everything that's in it?
  289. Yes, you can.
  290. So if you're on Mac, type L-S and hit enter.
  291. If you're on Windows, type D-I-R and hit enter, right?
  292. It's a little big here.
  293. Let me size that down.
  294. What's that?
  295. L-S. You know, I don't even know what L-S stands for.
  296. You know, there's this other crazy command, man, which will read for you the old manuals
  297. from like the 70s.
  298. So if we hit man, it's going to say list directory contents is what it was named originally.
  299. Why is that L-S?
  300. I have no idea.
  301. You know what I mean?
  302. You could write your doctoral thesis on this stuff.
  303. And so what you see is a list of the stuff in the directory and, oh man, I am really
  304. giving you guys a hard time.
  305. Sorry.
  306. And, you know, I bet you recognize some of these folder names that come up, right?
  307. You see like one called desktop or one called documents, right?
  308. I hate that.
  309. The documents one I really hate.
  310. It's just a mess, man, you know?
  311. And basically you're looking at the command line at the same stuff you see when you move
  312. around your computer in the interface.
  313. And we're just looking at it in this way and you can then inspect it.
  314. So let's say now we see that there's this directory desktop that's inside the directory,
  315. the PWD, where we're currently sitting, right?
  316. What if we want to move there?
  317. So in the same way that when you click on the desktop folder, it opens the desktop folder,
  318. right?
  319. You can issue commands that will move from your current directory to another one, right?
  320. And in this case, it's the same for both.
  321. It's that CD command from before.
  322. So what you can do is type CD and then the name of the folder you want to jump into,
  323. which in this case is desktop, right?
  324. And again, no, no bone, no bell, no reward.
  325. The computer just quietly moves on and returns the next line.
  326. That means it worked, right?
  327. That's a good sign when you're on the command line.
  328. Go ahead.
  329. Yep.
  330. Oh, hit Q maybe.
  331. Q. It worked?
  332. Okay.
  333. That's just the dumb stuff you memorize.
  334. So after like years, that's what you can look forward to is that knowledge.
  335. Hit Q to exit man because in 1977, blah, blah, blah.
  336. Okay.
  337. So, so what's happened?
  338. Now you did CD desktop.
  339. Now let's type our PWD again, or if you're on Windows, just CD and nothing.
  340. And guess what?
  341. My present working directory has changed, right?
  342. Remember before it was slash home slash Ben.
  343. Now it's slash home slash Ben slash desktop, right?
  344. So CD is the tool you use to jump around.
  345. Let's say I want to go back up to the one that's above me.
  346. That doesn't really have a name, right?
  347. That's sort of the parent of the one I'm currently in.
  348. There's another goofy way to do that, which you'll need to know, which is CD space dot
  349. dot.
  350. And dot dot is the magic notation for the parent of the directory you are currently
  351. in.
  352. Yeah.
  353. CD.
  354. And now I'm going to do PWD again and guess what?
  355. I'm back where I was before.
  356. It's in this kind of way that you can jump and move all over the file system, right?
  357. Let's say you wanted to move to an entirely different, you wanted to go back to the root
  358. of the file system and see everything from the very beginning.
  359. CD slash, right?
  360. L, S. Here's all the goofy stuff that's at the root.
  361. Then I could do CD slash home slash Ben, right?
  362. And I could type in the full path of someplace I would like to go, starting with the slash
  363. at the beginning, and that's going to jump me there.
  364. And it's with these types of acrobatics and just simple commands that you jump from one
  365. place to another and move around the file system.
  366. Make sense?
  367. Any questions about that?
  368. No?
  369. Cool.
  370. Okay.
  371. So now let's make some stuff.
  372. There's also a bunch of commands for creating and editing and deleting files, right?
  373. Like say a Python script, which we're going to get to, right?
  374. So let's say we want to make, so in our home directory we have desktop and we have documents
  375. and maybe you have like videos and music and all that crap that they load you up with,
  376. right?
  377. But we're done with that stuff, man.
  378. We're making code now.
  379. So we need a new folder called code to stick all our code in, right?
  380. And it's just as cool as all those other ones, so we're going to make it in the same
  381. place the same way, right?
  382. So did everybody CD dot dot back up to their home directory?
  383. And then do PWD or do a CD and look where you are and see if you're in that home thing.
  384. Yours won't be home slash whoever, unless you're on Linux like me, it'll be slightly
  385. different.
  386. It'll be whatever the Windows home directory is.
  387. PWD.
  388. So you might be out of your home directory.
  389. You know what?
  390. Let's do a cool shortcut.
  391. So there's actually a cool shortcut for your home directory that you can use at any time
  392. like ET to phone home, right?
  393. Or like Harry Potter to jump in a chimney.
  394. I use that for the kids now.
  395. All right, so if you do CD, a tilde, is that what you call in Spanish, too, a tilde?
  396. No?
  397. So if you do, it has no name?
  398. I've seen your keyboard.
  399. You have some pretty weird shit, too.
  400. No.
  401. But so you do CD, a tilde, and a slash, and that is kind of the universal signal for take
  402. me back to my home directory that you can use pretty much any time.
  403. That's not in the thing.
  404. So do that.
  405. And we can really put this code directory anywhere, so I wouldn't stress about it.
  406. But do CD, tilde, slash to make sure you're home.
  407. And now we're going to learn a new command.
  408. This new command will make directories, right?
  409. Make this code directory we want to put stuff in.
  410. All right, so first for the Mac people, and then for Windows.
  411. So the Mac command is MKDIR, right?
  412. And some weirdly named little compact thing that guess what stands for make directory.
  413. And then you follow that with a space and a name of the directory you would like to
  414. make.
  415. I'm going to do code with a capital C because it's just as cool as the desktop.
  416. Right?
  417. Oh, I already made code because I did this before.
  418. Hold on.
  419. This is what I get.
  420. I'm going to have to fix this real quick.
  421. Okay, so I'm going to name mine, I'm going to have to name mine code two because that's
  422. really professional.
  423. Just to show what you can do, right?
  424. So you do make dir code.
  425. Now if you're in Windows, you would do MD, which is a better abbreviation, I would argue.
  426. Right, guys?
  427. If we're writing these?
  428. Maybe not.
  429. I don't know.
  430. MD code is what you're going to do on Windows.
  431. Okay?
  432. That work for everybody?
  433. Oh.
  434. Whoo!
  435. Impressive.
  436. Impressive.
  437. That's a first.
  438. I've taught many of these classes, that's a first.
  439. Pretty cool.
  440. You're on your own.
  441. That should be Linux, though.
  442. So make dir should work.
  443. Though your problem might be that you don't have permissions to edit the file system because
  444. the phone's all locked down.
  445. Yeah.
  446. You might have the same issue.
  447. Yeah, you could try that.
  448. You guys can get that machine out if you want to.
  449. That's got admin on it, though.
  450. You're screwed.
  451. You might want to sit with somebody else, I guess.
  452. Sorry.
  453. I should have said that before we showed up.
  454. So did that work for everybody else on laptops?
  455. Okay, cool.
  456. So now if you do ls again, you're going to see your directory show up in that list.
  457. Okay?
  458. So now what we want to do is we want to move into our new directory.
  459. Who knows how.
  460. CD code.
  461. That's right.
  462. No, no, dot, dot, because dot, dot is backwards.
  463. So you just do CD code, and that'll move relative to your current directory.
  464. It will try to move into a directory named code.
  465. So everybody try CD code.
  466. And I have to do CD code 2 because I already have a code directory.
  467. Okay.
  468. No, no.
  469. Code 2 is just me.
  470. That's because I already had a code, so I had to make another one.
  471. That's how much I love code.
  472. All right.
  473. And, you know, obviously, it doesn't have to be code.
  474. It could be foo or donkey or whatever.
  475. You know, you can put whatever names you want in these cases.
  476. We're just choosing code.
  477. Okay?
  478. Awesome.
  479. Okay, so now we've moved into the directory.
  480. The next thing we're going to do is make a file there.
  481. And this is one you can only do on Mac and Linux that Windows doesn't actually let you do.
  482. So this is just going to be a quick Mac-only lesson.
  483. Let's say we want to make the Python file where we're going to store our scraper, right?
  484. There's a funny command named touch.
  485. I know, kind of creepy, right?
  486. Nerds.
  487. There's actually another one named finger.
  488. I'm not even making this up.
  489. So touch.
  490. And then we're going to name our file scrape.py.
  491. That's where we're going to stick all our Python code at the end of the day, and we're going to hit enter.
  492. Now, if you're in Windows, you could just open this directory in your My Computer.
  493. Go to your home directory.
  494. You're going to see code.
  495. You can just open it up there, and you can right-click and make a file named scrape.py.
  496. Have people done that before, just make a new file through My Computer?
  497. You have to save it yourself.
  498. You can't do it from the command line.
  499. Sorry.
  500. And so now if you do ls in this directory, you're going to see that the file you've made is sitting right there.
  501. And so touch is the way to make a file.
  502. Mine's kind of running off the end here because I got it so big.
  503. No problem.
  504. So have you cd'd into your code directory?
  505. Okay, now you need to open up My Computer and navigate to this new folder that we've made.
  506. You're in Windows.
  507. Or you're in Apple.
  508. Never mind.
  509. You want to do touch?
  510. You see the touch command right up here?
  511. Touch scrape.py?
  512. So what you're going to tell it to do with touch is you're saying, hey, computer, make a new file in this directory and name it scrape.py.
  513. Okay?
  514. And then if you ls after that, you're going to see it in the directory.
  515. And has anyone ever seen a .py file before?
  516. Do you know what that is?
  517. It's a Python file.
  518. And so all it is is it's just like any other file, any text file.
  519. It's empty.
  520. There's nothing in it.
  521. But the fact that we named it .py is when we get to the Python part going to ultimately be the instructions that let the computer know, hey, this is a Python script.
  522. And we need to, when we run it, we need to use Python.
  523. And the code in it is going to be Python code.
  524. And all that is just established by what you put on the end of the file when you name it, right?
  525. The same way an MP3 file is interpreted by the computer as music.
  526. And a JPG file, JPEG, is interpreted as an image, right?
  527. You guys have probably seen those before.
  528. .py is Python.
  529. Okay?
  530. Everybody get their file made?
  531. If you need help, just say so.
  532. You got it.
  533. So it's weird.
  534. I know.
  535. I know.
  536. I'm a little afraid to go into the man page on that one.
  537. And so that was one last thing we're going to learn.
  538. And then we'll be done with this, the kindergarten command line.
  539. Okay?
  540. And that's the most fun one.
  541. That's destroy a file.
  542. Right?
  543. Yeah.
  544. Yeah.
  545. Okay.
  546. Now, before I teach you how to do this, I'm going to warn you.
  547. Okay?
  548. This is not like, are you sure you want to put it in the recycle bin?
  549. And then even when you say yes, it's still in the recycle bin.
  550. And then they say, are you sure you want to delete it when you close it?
  551. You know what I mean?
  552. You get two chances on that recycle bin stuff, right?
  553. You get no chances.
  554. This is the most dangerous thing you do.
  555. You need to use it really carefully.
  556. If you were to write this command a certain way, which I will not teach you,
  557. all right?
  558. If you were to write this command a certain way, you could delete the entire
  559. file system of your computer and brick it.
  560. Right?
  561. And it's a big joke in computer programming to try to get people to do it,
  562. to hide it inside other code and screw with your friends.
  563. Right?
  564. But we're not going to go there.
  565. We're going to be really, really careful.
  566. Right, guys?
  567. We're just going to delete scrape.py.
  568. That's all we're going to do.
  569. We just made it.
  570. We're going to delete it.
  571. And the command for that is another dumb little thing.
  572. It's called rm.
  573. That's for Windows.
  574. That's not for Windows.
  575. That's for Mac only, Mac and Linux.
  576. And you do rm scrape.py.
  577. Hit it.
  578. Now if I do ls, guess what?
  579. It's gone.
  580. And you'll note it didn't ask me.
  581. Are you sure?
  582. It just did it.
  583. Okay?
  584. So if you're in Windows, I believe the command is del.
  585. Right?
  586. So you type del scrape.py.
  587. And then hit dir, right, to list the directory.
  588. And you'll see it's gone.
  589. Okay?
  590. Yeah, the command line.
  591. So those are the basic stuff.
  592. We're going to use a lot of these tricks as we get into the next stage.
  593. But that's basically the fundamentals of kind of basic command line stuff.
  594. Right?
  595. And there's one really special command we're going to need to get into Python stuff.
  596. But you might not have it already installed.
  597. Okay?
  598. And so that command is, guess what?
  599. Python.
  600. So we need to find out who has Python installed already.
  601. It probably comes preinstalled on your Apple computers.
  602. And who doesn't?
  603. It probably is not on your Windows computers.
  604. So could everybody please type Python, like the snake, right?
  605. Though actually it's named after Monty Python, I think.
  606. I hate to say it.
  607. Nerds.
  608. So type Python.
  609. Hit enter.
  610. And you see how my thing went into this weird little, like, land down here?
  611. This is the Python interpreter where you're able to write Python code.
  612. Really complex, important things that do amazing operations like 2 plus 2.
  613. Right?
  614. And if you got that, you've got Python.
  615. Right?
  616. And you're good to go on the Python part.
  617. If you don't have that, we need to install Python on your computer.
  618. Okay?
  619. So who doesn't have Python?
  620. All right.
  621. It's totally cool.
  622. It's all Windows people?
  623. Yes?
  624. Yes?
  625. Okay.
  626. Okay.
  627. We're going to have now a painful interlude where we try to get Python installed on the
  628. Windows computers.
  629. And we just got to do this and get it out of the way.
  630. It's going to be a pain.
  631. But once you have it, you'll be set.
  632. Okay?
  633. So I'm going to propose, since it's only a small minority of people, that we maybe take
  634. a 5 or 10 minute break and I just huddle with the Windows people and we work it out.
  635. And the Mac people don't have to deal with it.
  636. Does that sound okay?
  637. Okay.
  638. Good.
  639. All right.
  640. So, hey, guys.
  641. If you go back to that web page I made, Windows people, I'm talking to you.
  642. And you go to the top, that prerequisite section.
  643. Number three says this right here.
  644. This is going to tell us how we're going to install Python.
  645. And there's two steps.
  646. The first will be not so bad.
  647. The second's going to suck.
  648. Okay?
  649. But we're going to do it.
  650. All right?
  651. So the first is, is we need to go click on this link I have here where it says here.
  652. All right?
  653. You see that?
  654. And that's going to take you to the Python website where we're going to download something
  655. that will install Python for you.
  656. Okay?
  657. Okay.
  658. And my mouse has gone haywire so I'm going to restart my computer.
  659. But you guys all see that link and you're clicking on it?
  660. Okay.
  661. So do you see where it has a download?
  662. Now we need to find Python 2.7.
  663. We do not want Python 3.0.
  664. This is a whole lame Python thing that we're just going to dodge.
  665. Okay?
  666. 2.7.
  667. You see that?
  668. And it says download the Windows installer.
  669. Do you guys see that?
  670. The first that Windows x86 MSI installer.
  671. Does everybody see that right there?
  672. Okay.
  673. That's what we're going to download.
  674. And that's going to be a little click-through thing that's going to try to install Python
  675. for us.
  676. Okay?
  677. So can you just click that and download it?
  678. Thanks.
  679. Somehow my mouse has just, like, disappeared.
  680. It's totally bizarre.
  681. It must be something to do with the multi-screens.
  682. Oh, no.
  683. I got my battery filled up.
  684. Oh, that's this wireless thing.
  685. The mouse is back in Los Angeles.
  686. I'm trying to use the track pad.
  687. And I'm failing.
  688. You guys don't see it, do you?
  689. Totally weird.
  690. And maybe if I, is that thing downloading?
  691. So now you're going to have to click through it, okay?
  692. And let me look over somebody's shoulder as they click through it.
  693. Did you already do it?
  694. Oh, you got it?
  695. Okay, great.
  696. So we're going to open it up.
  697. Is it all downloaded for everybody?
  698. Okay, great.
  699. And it's going to open up some weird little thing that's like every other program you've
  700. installed in Windows, I think, right?
  701. Once it gets fired up.
  702. Okay.
  703. And it's going to say, should you allow it to do things to your computer, maybe?
  704. Right?
  705. And your answer has to be yes.
  706. Because we're playing with big boy's toys now.
  707. Okay.
  708. And then it's going to maybe ask you to select the destination directory where you're going
  709. to install Python.
  710. This is the folder where it's going to put it.
  711. Don't change this.
  712. Just say, yeah, I'm good with it.
  713. Hit next.
  714. Okay.
  715. Then it's going to ask you if you want to customize Python.
  716. You do not want to customize Python.
  717. Okay.
  718. Next.
  719. Okay.
  720. So it's going to install this.
  721. It may ask you to type in your administration password.
  722. If you don't have an administration password, you are screwed.
  723. To do this type of work, you need to be able to operate like a super user on your system.
  724. If your job is to try to do these things and your company gives you a computer that won't
  725. let you, you need to go to your boss and say, I can't do my job.
  726. Tell the IT idiot to take this off.
  727. Right?
  728. Because you won't be able to do the stuff we're going to try to do with this when you
  729. have those restrictions that are made for people who aren't programmers.
  730. Right?
  731. Okay.
  732. So let me open my terminal.
  733. Let me move it up over here.
  734. Okay.
  735. So again, let's type Python Windows, guys.
  736. Oops.
  737. Python.
  738. Okay.
  739. Did that happen?
  740. What happened?
  741. It doesn't work.
  742. Okay.
  743. So this is going to be what I expected and a teachable moment, guys.
  744. You ready?
  745. Okay.
  746. So these little weird things we're typing, LS, CD, PWD, Python, what the hell are they?
  747. Where are they?
  748. Right?
  749. They're little programs that are installed in your computer and they're off in some, like,
  750. distant hidden system folder that you probably have never been to or even knew existed.
  751. There's a folder filled with hundreds of these stupid little programs.
  752. Right?
  753. But they're not in the directory where you're issuing commands.
  754. They're off in this distant directory.
  755. So when you're executing them, you're not giving the full path to where they're at.
  756. You're just giving their names.
  757. So how does the computer know to run them?
  758. It knows to run them because there's this thing called the path.
  759. And I'm only teaching you this because you have to know it for Windows in this case.
  760. So the path is this wacky global variable that is on all these computers that's just a list of directories.
  761. Let me show you.
  762. This is really exciting stuff.
  763. And you can call it like that.
  764. Whoa, what's that, Ben?
  765. That's a list of directories in these hidden super folders separated by colons, like user local Ben.
  766. Once you're inside the mind of Dennis Ritchie in 1978, you'll know Ben stands for binary.
  767. Right?
  768. And let's say let's look inside that directory and see what's in there, right?
  769. Hey, it's a bunch of commands, right?
  770. And what if I grep like there's one that has LS in it, right?
  771. And if I echo the path again, I can see there's one that's just called Ben, right?
  772. And that has all kinds of stuff, including LS.
  773. So there is LS.
  774. LS itself is actually at slash Ben slash LS, right?
  775. But the reason why when you type LS, it actually calls that is your computer, every time you issue a command,
  776. is looking through this path for something that matches, that has the thing you just typed.
  777. And if it finds it, it executes it, right?
  778. So it's kind of a shortcut.
  779. So these things that are in these special folders you can call on without having to type their whole long location
  780. or to CD into their directory or any of that stuff.
  781. And Python's the exact same way, except Python and Windows is one we're adding that wasn't there before, right?
  782. And so Python itself is just a file, a file that was just installed.
  783. And remember when you were clicking through the goofy installer, it said, where do you want to put this?
  784. And it said, Python 2.7, it wanted to make that folder.
  785. You remember that in Windows?
  786. That's where your Python file is, except that folder is not in your path.
  787. So even though the file's there, when you type Python, it doesn't find it.
  788. So I know this is way in the weeds.
  789. So what you literally have to do to get Python to work on Windows, and remember, you chose Windows, not me, right?
  790. And this is true for almost all the programming languages.
  791. You have to install it, and then you have to edit your path so that it can find Python when you type it.
  792. I know that was a long journey.
  793. Did that make sense?
  794. Okay, Windows crew, we're going to edit your path.
  795. You already did it?
  796. I found a folder, Python 2.7.
  797. You already CD'd there?
  798. Dude, you're a pro.
  799. Right, so he CD'd into the Python directory, and within it found the Python thing, and then just called it,
  800. and because he was in the directory, it could find it.
  801. It didn't have to look on the path, right?
  802. And it just ran it.
  803. So okay, how about you Control-C to get out of that, or just type exit.
  804. Let's leave the Python.
  805. Right, and so now you're going to be exit, and then open parentheses, or Control-Z.
  806. Do Control-Z.
  807. Enter there.
  808. Now do CD, and let's see what directory you're in where you found the file.
  809. Just CD nothing, so it'll tell us where you are.
  810. You're in C colon slash Python 2.7.
  811. So C colon slash Python 2.7 is what we need to add to your path, right?
  812. So how do we add things to the Windows path?
  813. Oh, well, so let me write that down.
  814. So if I've got this right, tell me if I got this wrong.
  815. This is the folder where it is.
  816. Oh, I got Python spelled wrong.
  817. Write that, capital C maybe.
  818. Okay, so that's the directory we need to add to your Windows path.
  819. And so here's how you do it.
  820. So this could vary depending on your version of Windows.
  821. Yeah.
  822. So do you have a My Computer, like a little My Computer icon in your desktop,
  823. or is that too old for you guys now?
  824. If you do, could you right-click on it?
  825. So you're going to go to your My Computer, wherever that is, the little opener.
  826. And then the actual My Computer icon itself.
  827. Can you right-click on it?
  828. Okay, has everybody been able to do that?
  829. I see you were able to, right?
  830. This is what we get.
  831. Okay, so Windows users, right-click on My Computer,
  832. and then there's going to be something like Systems Properties,
  833. or what did it say?
  834. System.
  835. Okay, so you know where the My Computer is?
  836. Have you seen that?
  837. It's probably on your desktop.
  838. So My Computer itself, right?
  839. So there's an icon you clicked on to get here.
  840. That's your user folder.
  841. How about that guy?
  842. Right-click on that.
  843. Right-click on this one.
  844. Or right-click, so it'll bring up like a menu.
  845. Yeah, then do proper.ace.
  846. I get it right now.
  847. Not even close.
  848. Yeah, okay.
  849. Okay, so could you try doing that same thing,
  850. which is right-click on your computer right there?
  851. Yeah.
  852. And then go to Properties.
  853. I'm sorry.
  854. It has to be done.
  855. Okay, we got there?
  856. You got there?
  857. Okay, so who's got one in English so I can look over your shoulder?
  858. Did you right-click on My Computer?
  859. So you hit that,
  860. and then there's going to be a little computer icon if you scroll up.
  861. Scroll up.
  862. That guy.
  863. It says equipo on some of these.
  864. And then go to Properties.
  865. Okay, so that's going to give us this like crazy computer settings page.
  866. Does anyone have it in English so I can kind of like peek over your shoulder?
  867. Oh, thank you.
  868. So in the left-hand menu,
  869. I'm going to guess we're going to look in Advanced System Settings.
  870. And yes.
  871. So do you guys see something that says like Advanced System Settings?
  872. You click that, and then that's going to open up a weird gray box, okay?
  873. I know.
  874. Blame Bill Gates.
  875. Can you believe he's this rich?
  876. Okay, yes.
  877. Okay, so you guys got it up.
  878. Okay, and now there's a bunch of weird tabs.
  879. There's a bunch of weird tabs.
  880. Come on.
  881. You've done this before?
  882. Okay, so then there's an Advanced tab that's in the middle of the new thing that popped up.
  883. Can you hit that Advanced tab?
  884. Okay, awesome.
  885. Now at the very bottom of this dumb thing is something that says Environment Variables.
  886. Do you see that?
  887. Environment Variables?
  888. Let's click on that.
  889. There it is right there.
  890. Okay, did you get that up?
  891. Okay.
  892. Okay.
  893. Everybody's got that up.
  894. Okay, and so you should see in that top box a list of goofy variables, right?
  895. And so some people will have one that says Path and others won't.
  896. Is that what's going on?
  897. I see some people.
  898. So his says Path.
  899. Do you have one that says Path in the little list there?
  900. Maybe in the lower window.
  901. Let's look on your lower window.
  902. Oh yeah, so on some machines it might be in the lower window or the upper window,
  903. but look for the one called Path.
  904. Do you see that?
  905. Okay, so that is the same way I was printing out my path up on the screen.
  906. That's your path that leads to all the folders where the Windows files are.
  907. And so what we need to do is we need to add your Python directory to that list that's on the right-hand side there.
  908. And the way we're going to do it is we're going to click on Path in that little list, right?
  909. So it gets blue.
  910. And then we're going to hit Edit.
  911. Do you guys see that Edit button?
  912. Okay, this is pretty gnarly, I know.
  913. Okay, so now do not delete what's in there.
  914. Do not delete what is in there.
  915. Okay, we're going to add to it.
  916. Okay, so click into it and say scroll your mouse all the way to the far right-hand side, right?
  917. And so your cursor is at the end of that long thing that's been written in that box, okay?
  918. And we need to put in the separator that's going to separate one directory in the list from the next,
  919. which is a semicolon.
  920. Is that how you say that in Spanish?
  921. You guys know what a semicolon is?
  922. Okay, thank you.
  923. So at the very end, you're going to put one of those guys,
  924. and then you're going to put the path to our Python folder where we believe that bin file is, right?
  925. With the C, yeah.
  926. So you guys see this last line I have written here?
  927. I'll blow it up.
  928. See that line right there?
  929. You're going to add that to the end on the right-hand side.
  930. Beautiful.
  931. Did you add it on the end?
  932. Oh, it's already working? Great.
  933. So you must already be in there if you look.
  934. I bet if you look, it's already in there.
  935. I might need to do a capital P, I think, on this Python.
  936. Yep, that's perfect.
  937. You don't need the space.
  938. I don't know if it matters, but you don't need it.
  939. Okay, were you able to get it?
  940. You're stuck on...
  941. Okay, so go to...
  942. There's one called Advanced, maybe?
  943. Click through these tabs so I can see them.
  944. No, click right here.
  945. No, try here.
  946. Right here.
  947. Hit this thing.
  948. No, that's not it.
  949. Cancel.
  950. Try this one.
  951. Click try here again.
  952. You might have older windows, huh?
  953. It's not your fault.
  954. Hit the here.
  955. My Spanish is so bad.
  956. No.
  957. Check this real quick.
  958. Your hard drive.
  959. Let's try this one again.
  960. Just look at it real quick.
  961. Okay, once you have it in there, just hit okay and just save it.
  962. Okay, and then close and reopen your DOS prompt.
  963. So close the command line after you've saved it
  964. and reopen it by typing CMD into that directory.
  965. Cancel.
  966. Hit that.
  967. Try that again. I'm just confused.
  968. Let's look through these.
  969. This is like permission.
  970. We'll Google it as we get other people caught up here in a minute
  971. and I'll come back to you. I'm sorry.
  972. Basically, the Google search we need to do is
  973. set the path in Windows Vista, which is what you're running.
  974. Okay, and if you just Google that,
  975. someone will have the screenshots that will help you real quick.
  976. I'll look when I have a second, but that's what it's going to be.
  977. Okay, so everybody that just did that and reopened their terminal
  978. and typed Python, did it work?
  979. Yes, so type Python now again.
  980. Yes.
  981. What a relief.
  982. Yes, you have Python. Windows.
  983. We almost defeated it.
  984. Okay.
  985. Right.
  986. Okay.
  987. We're ready for the Python segment of our lesson.
  988. Okay, guys?
  989. And if you call up, you guys still have that web page up
  990. where all the lesson is?
  991. Now we're going to work through, and I know this is a little kindergarten,
  992. but it's good stuff.
  993. We're going to work through the fundamentals of Python
  994. in the same way we had worked through the fundamentals
  995. of the command line, right?
  996. So I'm going to help you
  997. just kind of understand some of the basics,
  998. show you some simple basic commands,
  999. and after we kind of got them figured out,
  1000. we're going to write our script, all right?
  1001. But first, we've got to deal with the basics.
  1002. And so, you know, the first thing about...
  1003. Remember our file scrape.py we made?
  1004. Right, so running a Python file,
  1005. which is just in a script like anything else, right?
  1006. God damn it.
  1007. Oh, my God, sorry.
  1008. It's just as simple as taking that Python command.
  1009. Mm-hmm, mm-hmm, mm-hmm.
  1010. All you're doing is...
  1011. Python file name.py
  1012. is then going to execute whatever file you've named
  1013. with the .py and run it, okay?
  1014. And that's basically how you end up running Python files,
  1015. now that we have them at the end of the day.
  1016. But the alternative is just to type in Python itself
  1017. and hit enter and get the Python interpreter, as it's known,
  1018. so you can execute Python code like 2 plus 2 directly, okay?
  1019. Am I cut off at the end there a little bit?
  1020. All right, something like that, okay?
  1021. And so I want everybody to type in Python
  1022. and get down to their interpreter.
  1023. Let's switch, sure.
  1024. Okay, sorry.
  1025. Okay, and so now we're no longer in the command line.
  1026. I mean, we're in the command line,
  1027. but we're not playing by the rules of the command line.
  1028. You know, there is no...
  1029. Right, so there is no more ls here.
  1030. You know what I mean?
  1031. There is no more cd, there is no more rm.
  1032. We're now not writing command line programs and scripts.
  1033. We're writing Python code, which has its own rules,
  1034. its own commands, its own bad names for things,
  1035. and its own kind of world.
  1036. But what's good about it is you can write these scripts
  1037. that from top to bottom do stuff and then rerun them.
  1038. But you got to know the fundamentals of things
  1039. before you can run them.
  1040. So is everybody who's able to get it going
  1041. dropped into Python?
  1042. And you know what you can probably do,
  1043. I just thought of it, it's the fastest family.
  1044. You cd, you do Python directly,
  1045. you do problems in front.
  1046. I just thought of this.
  1047. Sorry, I'm breaking all the rules.
  1048. Let's open up your command line real quick.
  1049. Let's cd into...
  1050. I'm feeling bad.
  1051. Oh, God damn it.
  1052. Ben.
  1053. So...
  1054. cd, this is shift, Python, 2.7, right?
  1055. And then type Python, and it's not on your path,
  1056. but you can just run it right there.
  1057. Okay, so let's just skip the path and you can keep rolling.
  1058. You're with us? Okay.
  1059. So everybody's in.
  1060. So Python, so like the first thing you need to know
  1061. is anybody know what a variable is?
  1062. Have you ever heard of this?
  1063. So a variable is like a little bag or a little name space
  1064. where you name something and then you assign to it a value.
  1065. You know what I mean?
  1066. So you create a label that exists within Python
  1067. that has something stored inside of it.
  1068. And so I'm going to create one called greeting,
  1069. and I'm going to create it by naming it.
  1070. I'm going to name it greeting.
  1071. I'm going to type equal.
  1072. I'm going to open this string and then I'm going to say,
  1073. hola.
  1074. Okay.
  1075. Can everybody try that?
  1076. So you can do greeting equal hola.
  1077. Yes.
  1078. So Python is now the same across all platforms,
  1079. which is a great thing about programming languages too.
  1080. They can be run on different systems and do the same thing.
  1081. And that's good.
  1082. Okay.
  1083. So it just again just kicks down to the next line.
  1084. There's no bells or whistles.
  1085. There's no reward.
  1086. But what you've just done is you've created a thing called greeting
  1087. and you've put inside it the phrase hola.
  1088. Exciting, right?
  1089. So now you can do stuff with it, right?
  1090. Now that you have it.
  1091. So you could do, there's a Python command
  1092. you might be interested in called print.
  1093. Can anyone guess what this one does?
  1094. So one of the great things about Python
  1095. is they kind of did a better job of naming things.
  1096. It has its own problems, but one of the big,
  1097. the emphasis when the language was created in the 90s
  1098. was to make something a little more legible.
  1099. Like let's just name it print instead of echo,
  1100. which is what it's called on the command line, right?
  1101. So if I say print greeting, look what comes out.
  1102. Okay.
  1103. So go ahead and try that.
  1104. So we have, we have this variable,
  1105. which we then could screw around with and do stuff with.
  1106. And so I can do, and I could say greeting, I could change it.
  1107. I could instead say greeting equal hello, right?
  1108. And now I've changed what it is.
  1109. And if I print greeting, it says hello now instead of ola.
  1110. Make it sense?
  1111. Okay.
  1112. So the variable is the thing on the left.
  1113. That's the label or the namespace as it is known.
  1114. Maybe not such a great name either.
  1115. The namespace greeting is where we are storing hello.
  1116. But what is, you know, hello.
  1117. Hello is what is, well, hello is a string.
  1118. Hello is a string of characters grouped together
  1119. and surrounded by quotation marks.
  1120. And the thing about programming languages is they allow things
  1121. only in certain data types.
  1122. Everything has to be very strictly defined as one of a set of data types.
  1123. And so one type of data that you can assign to a variable
  1124. is a string, which is some letters and characters surrounded
  1125. by single or double quotes.
  1126. And you can do double quotes if you prefer that.
  1127. I could do or single quotes greeting equal ola, right?
  1128. So just to make the point, let's make a whole bunch of strings, right?
  1129. So I already have greeting.
  1130. Let's make one.
  1131. Let's call it goodbye.
  1132. And I'm going to say later bro, right?
  1133. Because I'm from America.
  1134. And I'm going to hit enter and I can then print goodbye.
  1135. Right.
  1136. And then I'm going to make a third one and I'm going to call it.
  1137. And, you know, again, you can name these really anything, right?
  1138. So I'm going to name this favorite animal and I'm going to say it's donkey.
  1139. Right.
  1140. And then I can print favorite animal.
  1141. Right.
  1142. And it says donkey.
  1143. And then maybe I just want to make a variable called donkey
  1144. and put donkey inside of it just for fun.
  1145. So I can print donkey and see donkey because I love donkeys.
  1146. But you get the idea that this is just like any kind of string like this
  1147. can just be shoved into a variable and then accessed later.
  1148. And you're going to use this all the time once you get a script.
  1149. And it might not make sense at first, but once we have all these building blocks,
  1150. I promise you we'll make something.
  1151. Okay.
  1152. So there's other kinds of data, you know, it's just like math class, man.
  1153. You got strings with your characters, but you got integers.
  1154. Remember, integers.
  1155. So an integer is a number that doesn't have a decimal point.
  1156. Right.
  1157. And you don't need strings on those.
  1158. So I can make one and like, you know, it's going to be like math class.
  1159. I'm going to name the variable X.
  1160. Right.
  1161. Just like math class and X is going to be 99.
  1162. Right.
  1163. And there's no strings around it.
  1164. And because there's no strings around it and because it's a number,
  1165. it's that X now becomes an integer.
  1166. Right.
  1167. And I can do this thing called type.
  1168. Right.
  1169. And it'll tell me X is an integer.
  1170. Right.
  1171. And then I can do X plus two and get 101.
  1172. Right.
  1173. But if I do greeting plus two, you can't add strings and integers.
  1174. Ints is short for integer.
  1175. Right.
  1176. Another random abbreviation that they've done.
  1177. So is it the word integer in Spanish or what would it be?
  1178. Integre.
  1179. Integre.
  1180. Okay.
  1181. Close.
  1182. Okay.
  1183. And so because X is an integer and Python,
  1184. like all programming languages is picky about data types,
  1185. I was able to add it with another integer,
  1186. the number two and get the combined number X plus two equals 101.
  1187. Right.
  1188. And if I just type X, it'll tell me X is 99 and I can do multiplication.
  1189. I can do X times error.
  1190. Hold on.
  1191. X times X.
  1192. There's all sorts of stuff you can do with integers you can't do with strings.
  1193. Right.
  1194. So that's the second data type we're going to use from time to time as well as integers.
  1195. There's another one called float as in floating point decimal is what that's short for.
  1196. And so that's just the number that has a decimal point and values after it.
  1197. So that would be 99.9.
  1198. Right.
  1199. Would be a float.
  1200. Right.
  1201. And you could do another one and it could be 0.1.
  1202. And you can do Y plus Z then and you get 100 even.
  1203. Right.
  1204. So it's just another data type similar to integer.
  1205. It allows decimals.
  1206. They keep them separate.
  1207. It's a math thing.
  1208. Okay.
  1209. It comes up and when you do really high end math for what we're doing,
  1210. it doesn't matter.
  1211. But if you need the decimal points, you got to make sure things are floats now.
  1212. Now a really fun one that we're going to use all the time, which is called a list.
  1213. So if you guys done a lot of programming before, I don't know if it's okay.
  1214. If you haven't, sounds like not.
  1215. But every programming language has things like this strings, numbers, something similar
  1216. and lists and lists are just collections of variables or values that you can do stuff
  1217. with.
  1218. And the way you make a list is you just make one of these brackets that looks just like
  1219. that.
  1220. Okay.
  1221. It's got to be that rectangular bracket and you just start putting stuff in it.
  1222. So I'm going to make a list and I'm going to say 1, 2, 3, 4 are going to be my list.
  1223. And I hit enter and there's a list.
  1224. 1, 2, 3, 4.
  1225. And I can even put that in a variable.
  1226. I can say list of ints.
  1227. Right.
  1228. So it's a list filled with integers.
  1229. So I can now have a variable that has inside of it a list.
  1230. And you can do stuff besides that.
  1231. You know what I mean?
  1232. You could do 1, 2, A, B. Right.
  1233. That's okay.
  1234. You know what I mean?
  1235. So with a list can contain any of those data types that we've seen before.
  1236. A big list of them.
  1237. And it can even contain other lists.
  1238. You know what I mean?
  1239. I could have in here then 3, 4, 5.
  1240. And I now have a list with a list in it.
  1241. And this sort of nested lists and how we organize lists is a big way that we kind of structure
  1242. and transform data that we're moving around and playing with in our scripts.
  1243. Okay.
  1244. Am I going too fast?
  1245. No.
  1246. This makes sense.
  1247. Okay.
  1248. Good.
  1249. And then lists have cool things you can do with them, man.
  1250. So you remember I made list of ints.
  1251. Right.
  1252. So let's say I want to pull an item out of the list.
  1253. I want the number 1.
  1254. You can do another bracket like that on the end and then put a number in it and it will
  1255. pull the item at that number right out of your list.
  1256. So you're able to access the list, pull items out of it.
  1257. We're going to loop through it pretty soon and interact with it in ways you can't with
  1258. other data types.
  1259. And so why is 0?
  1260. Go ahead.
  1261. I was just about to say that.
  1262. You're kicking butt.
  1263. That's exactly right.
  1264. So remember there's all the weird stuff we have to memorize.
  1265. One of the crazy things about computers, and this is true with almost every programming
  1266. language, is that things are what are known as 0 indexed.
  1267. And so the first of anything is actually the 0.
  1268. So whenever you want to pull, so if I want to pull the first thing out of the list, I
  1269. give it the 0 index that's called.
  1270. If I want the second thing out of the list, I do the 1 index.
  1271. You just subtract 1 from whichever one you want mentally, and you can kind of get where
  1272. you want to go.
  1273. And let's say there is no fifth one, and so if I ask for an index that isn't in the list,
  1274. it just throws this error, right?
  1275. And so that's one of these Python things you just got to memorize, but it's really true
  1276. for all languages.
  1277. 0 comes first.
  1278. Count starting with 0.
  1279. One of these programming things, man.
  1280. There'll be exceptions to it too, which just drive you crazy.
  1281. So then you can do fun things like you can slice a list.
  1282. Let's say I want just the first two things in the list, right?
  1283. You do the same bracket thing.
  1284. You give it the index you want it to start at, a colon, and then the one you want it
  1285. to end at, and it will slice portions of the list in different ways so you could exclude
  1286. values and goof around with it.
  1287. Make sense?
  1288. We'll go over this again when we actually use it.
  1289. Okay.
  1290. Then there's this other one we don't really care about called a tuple, and a tuple is
  1291. a list that you can't add things to.
  1292. It's a permanent list that has to stay the way it is forever.
  1293. So I could take list of ints, for instance, and I could do append, add 5, right?
  1294. And then if I do, you'll see that now 5 is on the back end of the list, right?
  1295. So you're able with lists to edit them and goof around with them after you make them,
  1296. right?
  1297. Tuples you can't, right?
  1298. Who cares?
  1299. Boring, right?
  1300. So you only really need tuples.
  1301. Well, you don't really ever need tuples for what we're doing, but they're kind of there.
  1302. Maybe at some point it'll come up in your life, all right?
  1303. It's a list that you can't edit.
  1304. So for instance, if I take this and then I try to add something to it like I did the list,
  1305. it's going to throw an error.
  1306. And so it's basically a list that can do less than a list.
  1307. We probably shouldn't even cover it.
  1308. It's boring, okay?
  1309. There's a final data type, the dictionary, which is we're not going to use in this lesson,
  1310. but is ultimately very useful.
  1311. And it's like a combination of keys and values that you can store together in something.
  1312. And we won't even go into it in depth because it's kind of complicated.
  1313. But basically, you see how I have something here that's called like the phone book?
  1314. And it has pairs.
  1315. So there's two sets of data.
  1316. There's the name, Chinese takeout, and there's the phone number to call.
  1317. And then there's the other name, mom, and the phone number to call.
  1318. So essentially you create something kind of like a phone book where there's pairs of what are called keys
  1319. and values that you then can look up and goof around with later.
  1320. So I could say, give me mom, right?
  1321. And then out comes the number, that kind of thing.
  1322. But those are all the basic data types.
  1323. That's basically it, guys.
  1324. You've learned every data type and all the stuff that goes not only into Python,
  1325. but pretty much all computer programming.
  1326. And these really boring basic fundamentals that I've just showed you will really quickly
  1327. become things you can do a lot with, okay?
  1328. And so one way you start doing it is by chaining them together into what are called control structures
  1329. or scripts of multiple things on top of each other that negotiate a set of data and do stuff.
  1330. So the one we're going to use in our scraper that we'll cover now is called a for loop.
  1331. Have you guys ever done a loop?
  1332. So we have the list, right, and we want to loop through it,
  1333. and we want to do something to everything in the list, right?
  1334. I'm skipping some stuff in the lesson here too because I want to get to our scraper.
  1335. But you can look at the other stuff if you want to.
  1336. So I'm going to clear this, and I'm going to make a new list, and I'm going to call it letters, right?
  1337. And letters is going to have A, B, and C in it, okay?
  1338. And remember, I could do letters and zero, and I could get back A.
  1339. Makes sense, and I could do letters and two, and I could get back C.
  1340. Okay, so I can access it, but what if I want to loop through it and do something with every letter in the list?
  1341. Like say I might want to loop through a spreadsheet of data, right?
  1342. Huh? That's where we're going to go with that ultimately.
  1343. So what you do is there's another special command called for, spelled for,
  1344. and you do something like four letter in letters, right?
  1345. Hit colon, enter. You should get this dot, dot, dot. You need the colon.
  1346. Four spaces. One, two, three, four. One, two, three, four.
  1347. And then I'm going to do print letter, and I'm going to hit enter twice.
  1348. Look what it did.
  1349. So go ahead and try that. There's a bunch of stuff to unpack here.
  1350. So the magic words here, the magic words here that you will always reuse
  1351. whenever you loop through a list are for and in, right?
  1352. And then these are things you can substitute and do other stuff with.
  1353. So letters, right, is our list that we made up here.
  1354. And we're saying for every letter in the list, right, print the letter.
  1355. And then one, two, three, it prints the letters.
  1356. So what it's doing is it's looping through every item in the list
  1357. or iterating, as they say in programming, right?
  1358. It's looping through every item in the list, and it's doing what you tell it to here, right?
  1359. And if you've done this in other programming languages like JavaScript or Java,
  1360. you may have had to do like a parenthesis like here and here,
  1361. like around the clause that you've written to close off the code
  1362. that's being executed against each letter.
  1363. This is where Python is unique.
  1364. This is the one thing that's totally special about Python and that no other language does.
  1365. It's that whitespace, this space, space, space, space we can't do here,
  1366. basically created a whole other set of indented code
  1367. that only got ran inside the loop for each one, okay?
  1368. And the computer just actually reads your whitespace.
  1369. And so every single space you type in a Python code is meaningful
  1370. and will be interpreted by the computer as it executes the code, right?
  1371. And the indentation is a way that you block something off to say,
  1372. I only want it to run inside of, say, a loop, okay?
  1373. And this is a tricky thing because it means you've got to get your spaces just right.
  1374. You don't use tabs.
  1375. If you use four spaces, you always have to use four spaces for it to work.
  1376. If you have multiple lines in your loop, like I'll show you.
  1377. So let's say I do this again and I do four letter in letters, right?
  1378. Oops, I screwed up.
  1379. See how as soon as I made a typo, I got an error?
  1380. That's how picky this stuff is.
  1381. One, two, three, four.
  1382. Print the number one.
  1383. One, two, three, four.
  1384. Print letter, right?
  1385. And you see how within the loop, for each time through,
  1386. it executed the two things that it had been indented, right?
  1387. Print one, print letter.
  1388. First one's A.
  1389. Print one, print letter.
  1390. Second one's B, okay?
  1391. So we're able, with our indentation, to create kind of a level
  1392. or a bracketed off piece of code that only operates within that world.
  1393. Okay, so like here's another.
  1394. And then once you de-indent, you come out of that world.
  1395. So watch this.
  1396. So I do four letter in letters, right?
  1397. One, two, three, four.
  1398. Print letter, right?
  1399. Then I go down and I do print hello, right?
  1400. That just runs once.
  1401. Because we've come out of our little indented world, right?
  1402. And when I didn't have that space in there,
  1403. when I just tried to print it right after,
  1404. or if I tried to print it at a different indentation,
  1405. like if I tried just one space and I did print, print one,
  1406. it's going to break, right?
  1407. And it's going to say, you have an indentation error.
  1408. This thing you did right here, I don't know what to do with.
  1409. And the computer's so picky, that's how code is,
  1410. it's just going to fall apart.
  1411. And so the real finicky thing at first
  1412. is you have to get the white space just right.
  1413. And some people who don't like Python
  1414. call this white space fascism.
  1415. Yeah, that hurts, right?
  1416. Because it's very, very picky about how you line stuff up.
  1417. And so if you're getting errors,
  1418. that could very well be the cause,
  1419. and that's going to be really important
  1420. as we get more complex and we put more stuff together, right?
  1421. But this is the basics of how you loop through a list.
  1422. And now letters is the name of our list,
  1423. but this letter right here,
  1424. so this is a variable that we create
  1425. that then exists inside the indentation.
  1426. And this doesn't have to be letter,
  1427. we could name this whatever we want, right?
  1428. In the same way we were naming variables
  1429. when we first started.
  1430. So we could do, for instance,
  1431. I could instead say for donkey, right, in letters.
  1432. One, two, three, four.
  1433. Now what do I type?
  1434. Print donkey, that's right.
  1435. And so the way the loop works
  1436. is the things that always have to be the same
  1437. is this has to be a list, right?
  1438. This has to say for and in,
  1439. and then you have to name the variable
  1440. that it's going to be iterating through.
  1441. Okay, and that is how you loop through a list.
  1442. Are we out of time?
  1443. Oh my God, totally?
  1444. Are they going to kick us out?
  1445. Okay, can I just show you the scraper script really quick
  1446. even though we won't have time to write it together?
  1447. Guys, I'm sorry.
  1448. Okay, so, Boone County Jail, who's in lockup, right?
  1449. So they have this goofy website.
  1450. I'm going to go super duper fast.
  1451. Get excited, right?
  1452. So it is this webpage right here.
  1453. Oh, hold on, right here.
  1454. And so this is a dumb webpage
  1455. that they do in Columbia, Missouri every day
  1456. telling you who is currently in jail.
  1457. And it updates and looks the same every day.
  1458. It has their name and as their gender,
  1459. their race, their age, and where they're from,
  1460. mostly from Columbia, Missouri, right?
  1461. And so let's say in all this page,
  1462. I want this as a spreadsheet.
  1463. I want a scraper to go,
  1464. and every day while I sleep even, right,
  1465. download this spreadsheet and save it to a file.
  1466. So over time, I can build a database
  1467. of everybody who gets locked up, right?
  1468. This is a table that's inside this HTML thing,
  1469. and I want to scrape it out.
  1470. If I inspect it using this magic tool,
  1471. I can see that here it is.
  1472. This is the table that's inside the page
  1473. I want to pull the data from, right?
  1474. And I can see that it has a unique goofy name
  1475. called results table.
  1476. That's the name of this table.
  1477. And so what I need to do is write Python code
  1478. that's going to pull out results table,
  1479. loop through all the rows, use our looping skills,
  1480. grab the data, and print it out to the file, okay?
  1481. And so then the way that happens is you have to install
  1482. a couple other Python libraries,
  1483. and this was going to be the other hellish install
  1484. we don't have time for,
  1485. that allow you to download websites
  1486. and parse them using Python and make a script.
  1487. And so the script is right here, right here, right here, right here.
  1488. Oh, too big.
  1489. Right?
  1490. And so this is a Python code we'd write in that script
  1491. that does a lot of the basic things we talked about.
  1492. What we didn't get into is you can import these other libraries
  1493. that do all kinds of magic things so you don't have to write code.
  1494. One will download websites, the other will parse them.
  1495. This one will write them out to a spreadsheet, right?
  1496. And then what you do is you just start using them.
  1497. You make a variable, right, called URL,
  1498. which includes a string,
  1499. which is the URL of the web page you want to visit.
  1500. You then use the fancy library we imported right here
  1501. to get the URL and save it
  1502. and pull the HTML from the web thing magically in the script.
  1503. We then pass that into this other library that can parse the HTML, right?
  1504. We give it the special instructions and we say,
  1505. hey, find in that the table called results table.
  1506. Remember how we saw that in the page?
  1507. And that's the detective work of scraping,
  1508. is that you have to find in the page the unique thing
  1509. that allows you to pull the string and extract the data,
  1510. which we did really quickly inspecting it.
  1511. There it is.
  1512. That table is now a thing that we can find stuff in using beautiful soup.
  1513. So I say, hey, I want you to take the table
  1514. and I want you to find every row in the table,
  1515. which happens to have an HTML tag called TR, which stands for row.
  1516. And guess what, I want you to loop through it
  1517. just like we just did, right, guys?
  1518. So for every row in the HTML rows, right,
  1519. I want you to then do a second loop.
  1520. I want you to find all the cells in the table loop through the cells, right?
  1521. I want you to grab the text out of them
  1522. and then I want you to add them to a list, right?
  1523. So as we're looping through the cells,
  1524. we're adding them to the list and that's a row
  1525. and then add the row to the big list of all the rows
  1526. and then we create a list of lists
  1527. that has all the data pulled from the HTML via the loop, right?
  1528. And then write it out to a spreadsheet using the simple spreadsheet tool.
  1529. I know that was really fast, but that's basically where we're going.
  1530. If you follow on the web page, which again is firstwebscraper.rtfd.org, right?
  1531. You guys remember that one?
  1532. That's this one.
  1533. Every single step is explained and written out
  1534. and it will show you how to work through the skills you have now
  1535. to write the script and do exactly what I just showed you.
  1536. So we didn't have time to get through all this stuff tonight.
  1537. We only got you set up and running, but you can go home,
  1538. click on Act 3 and you can do all that stuff
  1539. and end up with the script that works
  1540. and there's step-by-step instructions.
  1541. So the class can continue even though they may kick us out of here.
  1542. Okay, anybody have any questions or you want to yell at me
  1543. for not going fast enough?
  1544. Yeah.
  1545. So there are utilities in Python to interact with all sorts of things.
  1546. You know, so there's millions of open source libraries
  1547. or thousands that will make it easy to access different things
  1548. like requests and beautiful super we just saw.
  1549. And there are tools for accessing Twitter and Facebook
  1550. that allow you to, just in the same way we import requests,
  1551. you sort of import Twitter or whatever it's called.
  1552. And these things, you just Google them on the web,
  1553. you look for them, Python and Twitter,
  1554. and you'll see that there's libraries for interacting
  1555. with all sorts of different stuff that have in them functions
  1556. and tools that allow you just to stuff in the data you want
  1557. and kind of make something simple by smashing them together.
  1558. Yes.
  1559. There's not necessarily a best one.
  1560. I like Python just because I like Python because I like the simplicity.
  1561. I think that the white space and a lot of the stuff makes it easy to read
  1562. and that's why Python is often taught to beginners,
  1563. especially even children that taught Python as a first programming language
  1564. because a lot of the stuff we were just doing is really simple
  1565. and you'd be surprised how kind of weird it gets in other languages.
  1566. But to be honest, you can do this with any of the really popular languages.
  1567. I can't say it's necessarily better.
  1568. I just think it's readable.
  1569. Oh yeah, it's all free.
  1570. I mean, remember we downloaded and installed it.
  1571. Everything's free and open source.
  1572. I mean, all the major programming languages are,
  1573. with the exception of Java and the Windows stuff.
  1574. But unless you need to make a Windows app,
  1575. there's no reason to bother with that stuff.
  1576. So it's usually best just to, if you can,
  1577. to just code your own web scraper rather than using, you know,
  1578. kimono labs or import.
  1579. Like the point and click tools?
  1580. Oh, those are great.
  1581. You know what I mean?
  1582. I like them and I think that if it's like a browser plug-in,
  1583. they can do it and you can just grip and rip the data,
  1584. like I like to say, just go for it.
  1585. I mean, you don't need to write code when you don't need to.
  1586. But this is the foundation from which you'll be able to execute
  1587. much more complex scrapes.
  1588. So for instance, you could take this and then automate it
  1589. and set it to run every day at 5 a.m.
  1590. and gradually build a list over a year,
  1591. where if you use a browser plug-in,
  1592. you'd have to go and click it every day at 5 a.m., right?
  1593. So you're able to make it repeatable more easily.
  1594. And then also there's some things you want to scrape,
  1595. and these are the ones that are the most fun,
  1596. that are like paginated.
  1597. So let's say you want to scrape a web page
  1598. that has 5,000 pages of data that you have to go through, right?
  1599. You'd have to go with your browser plug-in,
  1600. you'd have to go and click and click and click,
  1601. which what we could do with this,
  1602. and there's a lot of more advanced scrapes you can look on online
  1603. and if you email me, I can send you some,
  1604. that basically will then click through all the pages
  1605. and repeat the same scrape for every single page
  1606. and then you're able to download hundreds of thousands of records
  1607. by basically just visiting every page on the website
  1608. and extracting the data you want.
  1609. But before you can get there, before you can run,
  1610. you've got to learn to walk.
  1611. But if you just need something one time,
  1612. smash and grab, I just want it, yeah, the plug-ins are great.
  1613. Another just short question.
  1614. In any case, is web scraping illegal?
  1615. Yes.
  1616. If it's a government website, almost certainly no,
  1617. because it's public information.
  1618. If it's a private website that has a terms of service or whatever,
  1619. they oftentimes don't want you to do it.
  1620. Now, there's a difference between collecting it for your own private use
  1621. and republishing any of it legally, and I'm not a lawyer,
  1622. but I think you can scrape things that are private data
  1623. and I think maybe legally argue in some cases
  1624. you're not breaking the law if you just store it
  1625. and don't do anything with it, but if you say republish it, you issue...
  1626. Do you guys have Craigslist here?
  1627. Yeah, so it's classified ads for apartments.
  1628. They don't want anyone scraping them
  1629. because people could scrape them and make a competitor
  1630. or do game the system or something.
  1631. And so they want to make it against the rules of their website,
  1632. which gives them the right to ban you or potentially sue you or whatever,
  1633. and also will develop software that will try to stop you from doing it.
  1634. So they'll see that you're visiting every page
  1635. and they'll kill your request or ban you kind of automatically.
  1636. Also, the same technology that issues this request...
  1637. So hackers are pretty famous now, right? They're everywhere.
  1638. Have you ever heard the phrase denial of service attack?
  1639. Right, so that's basically when you...
  1640. It's the most basic hacker attack.
  1641. It's what Anonymous does to MasterCard or whatever.
  1642. And all it really is is requesting as many web pages as you can
  1643. as quickly as possible so that the web server
  1644. that passes the page gets overwhelmed and crashes.
  1645. So it's just request, request, request, request, request, request,
  1646. request as many times as you want.
  1647. The technology that does that is no more complicated than this.
  1648. It's literally code this simple that makes those requests.
  1649. It just does it many, many times in a row
  1650. and distributed across many different machines,
  1651. which is what a distributed denial of service attack is.
  1652. So if you were to run this Python script over and over again,
  1653. or let's say you were to make a loop,
  1654. you were to make a list that had the number 1 through 1 million,
  1655. 1 million numbers in the list,
  1656. and you were to say, 4 number in, 1 through 1 million, colon,
  1657. space, space, space, this,
  1658. you would then issue 1 million web requests in a row
  1659. for the exact same URL over and over again.
  1660. And you would essentially be running a low-grade denial of service attack.
  1661. Maybe not even intentionally, but you would.
  1662. And so sometimes if it's a crappy government website,
  1663. they'll crash just because they're poorly built
  1664. and they can't handle it and you don't mean it.
  1665. And so what you can do is you can put in delays.
  1666. There's a library called time,
  1667. and you can say inside that loop, 1 through 1 million,
  1668. you could put time.sleep 2 seconds.
  1669. Every time it went through the loop, it would make the request,
  1670. wait 2 seconds, start the loop again.
  1671. Make the request, wait 2 seconds, start the loop again.
  1672. And so with tricks like that, you can kind of make it easier
  1673. on the people you're scraping, and it's kind of polite scraping,
  1674. which I would recommend.
  1675. So don't use this.
  1676. So what I've just told you,
  1677. you could basically be a crappy member of Anonymous at this point
  1678. with that basic technology.
  1679. And as you get more into programming,
  1680. you'll learn a lot of this stuff once you understand it.
  1681. It's not that complicated.
  1682. It's just all these basic data types and loops
  1683. and things that we're going through.
  1684. The last question.
  1685. Thank you.
  1686. If we want to learn more Python, what would you recommend?
  1687. Well, there's a million things on the Internet.
  1688. You know what I mean?
  1689. Google is great for this.
  1690. Well, Google will lead you to many things that are great for this,
  1691. which is you just type in...
  1692. So there's free books that will teach you Python.
  1693. There's one called Dive into Python.
  1694. There's another called How to Think Like a Computer Scientist or something.
  1695. But what they really are is kind of kindergarten Python.
  1696. And they'll teach you actually a lot of the stuff that we just did.
  1697. You know, I'm a member of IRE-NICAR,
  1698. which is a group at the University of Missouri where that jail is.
  1699. And they have a lot of training materials for Python and other languages
  1700. that have kind of journalism examples.
  1701. So if you just type in Python and journalism,
  1702. and you'll find probably a bunch of stuff.
  1703. I can't think of a ton off the top of my head,
  1704. but there are literally hundreds of thousands of things to look at.
  1705. I would say like a beginning Python book would be good.
  1706. But the reality is that computer science,
  1707. you could study it for years and years and years.
  1708. It's very deep.
  1709. What you really need is a combination of the fundamentals
  1710. and then kind of a practical project,
  1711. like a web scrape that you want to work on
  1712. and to partner the two things, and you'll get there.
  1713. Okay?
  1714. Thanks, guys.
  1715. I know that was the crash course.
  1716. That was a crash course, but we survived it, huh?
  1717. Thank you very much.
  1718. Okay.

Downloads

Recording video · Timestamped transcript