Latest / CyberSecurity Summary / Instant PHP Web Scraping
Transcript
- 0:00Speaker 1: Okay, welcome to the deep dive. Today. We're jumping into webscraping.
- 0:04Speaker 2: That's right, specifically using PhD based on some key ideas
- 0:09Speaker 2: from roller source Instant PHPE Webscraping.
- 0:12Speaker 1: Yeah, it's from twenty thirteen by Jacob Board. So things
- 0:15Speaker 1: have moved on obviously, but the core ideas they're often
- 0:19Speaker 1: still relevant, aren't.
- 0:20Speaker 2: They They really are. The book was aimed at beginners,
- 0:23Speaker 2: you know, showing how to programmatically crawl websites, download content,
- 0:27Speaker 2: and basically turn unstructured web stuff into structured data using
- 0:32Speaker 2: using PHP.
- 0:33Speaker 1: So our mission here is to pull out those fundamental
- 0:35Speaker 1: techniques from these excerpts give you a solid grounding in
- 0:38Speaker 1: how PHPU webscraping works at its core.
- 0:41Speaker 2: Even if you're maybe adapting these ideas for more modern
- 0:44Speaker 2: sites later on, the basics often carry.
- 0:47Speaker 1: Through exactly the source assumes. Maybe not a ton of
- 0:50Speaker 1: programming experience, though knowing some PHP and HTML definitely.
- 0:53Speaker 2: Helps, sure, But the focus is really on the scraping
- 0:56Speaker 2: concepts themselves.
- 0:57Speaker 1: All right, let's kick things off. Before you scrape, you
- 0:59Speaker 1: need your tools. What's the basic toolkit according to these sources?
- 1:03Speaker 2: Okay, so first, obviously you need PHP itself that's the language, right,
- 1:08Speaker 2: then a good place to write your code an ID
- 1:11Speaker 2: integrated development environment. The source mentions Eclipse PDT.
- 1:16Speaker 1: PDT being the PHP development tools for Eclipse, so a
- 1:19Speaker 1: specialized code editor.
- 1:20Speaker 2: Yeah. Basically makes coding easier, keeps things organized. And then
- 1:24Speaker 2: you need a way to run the PHP and.
- 1:26Speaker 1: Probably a database too, like a local server setup exactly.
- 1:30Speaker 2: The source recommends xm yp It bundles a patche which
- 1:34Speaker 2: is the web server, PHP and myseqel the database all
- 1:37Speaker 2: in one package.
- 1:38Speaker 1: Ah. Convenient avoids installing everything separately.
- 1:41Speaker 2: Yeah, and it even includes php I admin oh.
- 1:44Speaker 1: Right for managing the myseql database visually useful later definitely Okay,
- 1:48Speaker 1: So you install XAMPP maybe Eclipse any specific setup tweaks needed.
- 1:53Speaker 2: A couple of key things. The source points out. Setting
- 1:56Speaker 2: your PHP path variable is good practice. Lets you run
- 1:58Speaker 2: PHP scripts easily from the command line for testing and stuff, right,
- 2:01Speaker 2: But the really critical one for scraping is enabling the CURL.
- 2:05Speaker 1: Extension curl Okay, what is that? Exactly?
- 2:08Speaker 2: It's a PHP library. You need it enabled in your
- 2:10Speaker 2: main PHP can fig file the PHP dot i ne
- 2:14Speaker 2: Without it, your PHP script can't really make web requests easily.
- 2:18Speaker 1: Ah, So it's essential for fetching pages programmatically.
- 2:21Speaker 2: Absolutely, And then you know, just test this setup, make
- 2:23Speaker 2: sure a patche runs. Maybe run a simple finfo script
- 2:26Speaker 2: to see if curl is listed as enabled.
- 2:28Speaker 1: Got it, so tool get ready curl enabled. Now the
- 2:32Speaker 1: first actual step in scraping getting the web page.
- 2:36Speaker 2: Fetching the content. Yeah, this is where CRL comes into
- 2:39Speaker 2: play directly.
- 2:40Speaker 1: Because it handles HTTP requests.
- 2:42Speaker 2: Exactly unless your script act like a browser, essentially sending
- 2:46Speaker 2: a request to a URL and getting back the HTML
- 2:48Speaker 2: source code.
- 2:49Speaker 1: And the source provides a function example curl get. What's
- 2:52Speaker 1: the basic flow there, it's pretty logical.
- 2:53Speaker 2: You initialize a CRL session, think of it as opening
- 2:56Speaker 2: a connection channel. Okay, Then you set options for that session,
- 2:59Speaker 2: tell it what you want to.
- 3:00Speaker 1: Do, like the URL you want to fetch.
- 3:01Speaker 2: Curl opterill is the main one, and critically kurl opter
- 3:04Speaker 2: own transfer. You usually want that set to true. Why
- 3:07Speaker 2: is that so? That's url returns the page content as
- 3:10Speaker 2: a string variable in your PHP script instead of just
- 3:13Speaker 2: like printing it straight to the screen. You need it
- 3:16Speaker 2: as a variable to work with.
- 3:17Speaker 1: It, right, makes sense? Any other key options?
- 3:20Speaker 2: Oh yeah, curl opc fallow location is super.
- 3:23Speaker 1: Useful for redirects like three ozho One's exactly.
- 3:26Speaker 2: Websites often redirect you. This option tells the URL to
- 3:29Speaker 2: automatically follow those redirects to the final page. Saves you
- 3:32Speaker 2: a lot to hassle. Nice.
- 3:34Speaker 1: What about curl op twuser agent. The source gives them
- 3:36Speaker 1: an example string ah.
- 3:38Speaker 2: The user agent. It's basically a string that identifies your
- 3:41Speaker 2: client your script to the web server one well, partly politeness,
- 3:45Speaker 2: partly necessity. Some servers block requests that don't have a
- 3:48Speaker 2: user agent string that looks like it's from a normal
- 3:50Speaker 2: web browser, so sending one makes your script look less
- 3:53Speaker 2: like a basic.
- 3:53Speaker 1: Bot okay, helps avoid immediate blocks potentially.
- 3:56Speaker 2: Yeah. Then there's curl optt header if you need to
- 3:59Speaker 2: send custom headers sometimes needed for specific sites and curl
- 4:02Speaker 2: up tfell on error.
- 4:04Speaker 1: What does that do?
- 4:05Speaker 2: It tells curl to treat HDP air codes like four
- 4:08Speaker 2: O four not found or five hundred server error as
- 4:11Speaker 2: well actual.
- 4:12Speaker 1: Script errors instead of just returning an empty page or
- 4:15Speaker 1: an air page.
- 4:15Speaker 2: Right, it can be a simple way to detect if
- 4:18Speaker 2: the request failed badly okay.
- 4:20Speaker 1: And the source mentions checking the HTTP response code itself.
- 4:24Speaker 1: Using curl jet info.
- 4:25Speaker 2: Why bother because knowing the code tells you exactly what happened.
- 4:29Speaker 2: Two hundred oka means success, you have the page.
- 4:31Speaker 1: Four oh four means it doesn't exist, right crucial info
- 4:35Speaker 1: A four h three forbidden means you don't have permission.
- 4:38Speaker 2: Maybe you need to log in or your IP is blocked.
- 4:40Speaker 1: Three oh one moved permanently, which follow location handles, but
- 4:44Speaker 1: good to know.
- 4:45Speaker 2: Yeah, checking the status code is fundamental for robust error handling.
- 4:49Speaker 2: You know why something might have failed.
- 4:50Speaker 1: Okay, so CRL gets you the raw HTML, maybe a
- 4:54Speaker 1: massive string of code. Now the real challenge finding the
- 4:57Speaker 1: specific bit of data you want inside all that.
- 5:00Speaker 2: Extraction time and the main tool the source introduces here
- 5:03Speaker 2: is XPath XPath.
- 5:04Speaker 1: I've heard of it with XML. How does it apply
- 5:06Speaker 1: to HTML?
- 5:07Speaker 2: Well, HTML isn't always perfect XML, but it's structured right
- 5:10Speaker 2: with tags and attributes. You can parts that downloaded HTML
- 5:14Speaker 2: string into something called a DOM a document object model.
- 5:17Speaker 1: A tree structure of the page.
- 5:19Speaker 2: Precisely, an XPath is a language specifically for navigating that
- 5:22Speaker 2: tree and selecting nodes elements attributes text based on their
- 5:27Speaker 2: path or characteristics.
- 5:29Speaker 1: So it's more structured than just like searching for keywords
- 5:32Speaker 1: in the string much more.
- 5:33Speaker 2: The source shows a function return XPath object. It basically
- 5:36Speaker 2: takes the HTML strength.
- 5:38Speaker 1: So when you got from crl right, it.
- 5:39Speaker 2: Uses PHPs built in don document class to load that HTML,
- 5:43Speaker 2: even if it's a bit messy.
- 5:44Speaker 1: I see the source us as an AT symbol before
- 5:46Speaker 1: load HTML. Is that related to MESSYHTML?
- 5:49Speaker 2: It is? Real world HTML often have minor errors. The
- 5:53Speaker 2: AT symbol in PHP suppresses warnings that load HTML might
- 5:56Speaker 2: generate because of that imperfect markup. It stops your script
- 6:00Speaker 2: potentially halting on minor issues.
- 6:01Speaker 1: Ah a practical trick for scraping. Okay, so don document
- 6:04Speaker 1: lugs the hhamel than what.
- 6:05Speaker 2: Then you create a dom XPath object from that DOM document,
- 6:08Speaker 2: and that XPath object is what you use to run
- 6:10Speaker 2: your queries.
- 6:11Speaker 1: Okay queries. The source has examples like h one or
- 6:14Speaker 1: span at class some class exactly.
- 6:16Speaker 2: Those are XPath expressions. H one means find any H
- 6:19Speaker 2: one element anywhere in the document.
- 6:21Speaker 1: And the span it class.
- 6:22Speaker 2: That's more specific, find any span element that has an
- 6:25Speaker 2: attribute named class with the exact value some class.
- 6:29Speaker 1: What about at href at.
- 6:31Speaker 2: The end of one example that's selecting an attribute, so
- 6:33Speaker 2: maybe it found a specific link attag and added to
- 6:36Speaker 2: rev says get the value of its h ref attribute
- 6:39Speaker 2: the url itself.
- 6:40Speaker 1: So you run these queries against the XPath object and it.
- 6:43Speaker 2: Gives you back a list of matching nodes, elements.
- 6:46Speaker 1: Or attributes, and then the source shows item zero node
- 6:49Speaker 1: value to get the actual text.
- 6:51Speaker 2: Right, the query might find multiple matches, so item zero
- 6:54Speaker 2: usually gets the first one in the list. Then node
- 6:56Speaker 2: value extracts the text content from inside that element.
- 7:00Speaker 1: Okay, so XPath is powerful for navigating that structure.
- 7:03Speaker 2: Very The source has a table with common expressions eight
- 7:06Speaker 2: headed taro apro using brackets for conditions. That's your vocabulary
- 7:10Speaker 2: for building these queries.
- 7:11Speaker 1: What if the data isn't neatly inside a tag or
- 7:14Speaker 1: the structure's just chaotic, XPath might not work then.
- 7:17Speaker 2: Exactly, Sometimes XPath is overkill or just plain impossible. That's
- 7:22Speaker 2: where as the source shows, you might need more direct
- 7:25Speaker 2: approach custom functions.
- 7:26Speaker 1: Like the screen between function mentioned.
- 7:28Speaker 2: Perfect example, it's much simpler conceptually. Its whole job is
- 7:33Speaker 2: to find a chunk of text that sits between two
- 7:36Speaker 2: other known unique strings, so you.
- 7:38Speaker 1: Don't care about HTML tags, just find the text after
- 7:42Speaker 1: start marker and before end marker.
- 7:44Speaker 2: Precisely, you give it the whole chunk of text, like
- 7:46Speaker 2: the page source, the starting string and the ending string.
- 7:49Speaker 1: How does it work.
- 7:50Speaker 2: It uses basic PHP string functions stripos to find the
- 7:54Speaker 2: position of the start and end markers, then subscripted to
- 7:58Speaker 2: cut out the piece of the string between those positions.
- 8:01Speaker 1: Simple but effective if the markers are reliable. The source
- 8:04Speaker 1: uses scraping a Google Analytics ID as an example.
- 8:07Speaker 2: Yeah, that ID is often embedded in JavaScript between specific
- 8:10Speaker 2: quote marks or function calls. XPath wouldn't easily grab that,
- 8:14Speaker 2: but scrape between works perfectly.
- 8:16Speaker 1: Okay, so we have text extraction covered with XPath and
- 8:19Speaker 1: custom functions. What about non text content images?
- 8:22Speaker 2: Good question. You often need to grab images too. The
- 8:25Speaker 2: process combines things we've discussed how So, First you usually
- 8:28Speaker 2: find the images url using XPath. You look for an
- 8:32Speaker 2: mg tag and grab its src attribute.
- 8:34Speaker 1: So like mng at src exactly.
- 8:37Speaker 2: That gives you the URL of the image file.
- 8:39Speaker 1: Then you use currl again.
- 8:41Speaker 2: Yep, you use your curl git function or similar to
- 8:45Speaker 2: download the content at that image url.
- 8:47Speaker 1: But this time you're expecting image data, not HDML.
- 8:50Speaker 2: Right, binary data and the source suggests a good practice.
- 8:54Speaker 2: Verify it actually is an image before saving it. How
- 8:58Speaker 2: PHP has a function get image. You can pass it
- 9:01Speaker 2: to the downloaded data or the filepath. If you save
- 9:04Speaker 2: it temporarily, it'll return image dimensions if it's valid or
- 9:07Speaker 2: false if it's not a recognized image type.
- 9:10Speaker 1: Smart So you verify it and then then you.
- 9:12Speaker 2: Just use standard PHP file functions. So open to open
- 9:15Speaker 2: a local file for writing, right to write the image
- 9:18Speaker 2: data you got from CURL into it, and F close
- 9:20Speaker 2: to close.
- 9:21Speaker 1: The file, and you've saved the image locally.
- 9:23Speaker 2: You have and that basic method find url, download with CRL,
- 9:27Speaker 2: save with file functions works for other file types too,
- 9:29Speaker 2: like pds or whatever.
- 9:30Speaker 1: Okay, fetching static stuff in images is one thing, but
- 9:33Speaker 1: lots of data is behind logins or search forms. Yeah,
- 9:36Speaker 1: how do you interact with sites like that?
- 9:38Speaker 2: Yeah? This requires simulating form submissions. Forms often use the
- 9:42Speaker 2: HTTP post method to send data.
- 9:45Speaker 1: So you need to make post requests with curl exactly.
- 9:49Speaker 2: The source shows a CURL post function example for this.
- 9:52Speaker 1: What do you need to know to make that POC
- 9:54Speaker 1: request work?
- 9:55Speaker 2: You have to inspect the HTML form on the actual
- 9:58Speaker 2: web page. First, look for the the form tag itself.
- 10:01Speaker 2: You need its action attribute. That's the URL you send
- 10:04Speaker 2: the post request to.
- 10:06Speaker 1: Okay, the destination you arel yep.
- 10:07Speaker 2: Then you need to find all the input elements inside
- 10:10Speaker 2: that form and select or text area too potentially what
- 10:13Speaker 2: about them? You need their name attributes. Those names become
- 10:16Speaker 2: the keys in the data you send, and you need
- 10:19Speaker 2: the value you want to send for each name.
- 10:20Speaker 1: So if there's an input name username, you send your username.
- 10:24Speaker 2: Right And crucially don't forget hidden input fields. They often
- 10:27Speaker 2: contain important stuff like session tokens or form IDs that
- 10:31Speaker 2: the server expects back. The source login example mentions needing
- 10:34Speaker 2: email password, but also destination and format which might be
- 10:38Speaker 2: hidden fields.
- 10:39Speaker 1: Ah, I got to check the source carefully. What about
- 10:41Speaker 1: login specifically? Don't they involve cookies?
- 10:44Speaker 2: Absolutely vital When you log in successfully, the server usually
- 10:48Speaker 2: sends back cookies to track your session for subsequent request
- 10:51Speaker 2: or restricted pages. You need to send those cookies back.
- 10:53Speaker 1: How does Kira r L handle that?
- 10:55Speaker 2: It has options for it? Curl up pokie jar tells
- 10:58Speaker 2: Curl to save cookies receives into a specified file. Cookie
- 11:03Speaker 2: file tells Curl to read cookies from a file and
- 11:06Speaker 2: send them with the request.
- 11:07Speaker 1: So you log in, save the cookies, and then use
- 11:10Speaker 1: those cookies for future requests to stay logged in.
- 11:12Speaker 2: That's the basic idea. It maintains your session state.
- 11:15Speaker 1: The source also mentions posting files like simulating and upload.
- 11:19Speaker 2: Yeah. If a form has an input type file, you
- 11:23Speaker 2: can simulate uploading a file using curl ob post fields.
- 11:27Speaker 2: You set the value for that field name to the
- 11:29Speaker 2: path of your local file, but you prefix the path
- 11:32Speaker 2: with an AT symbol.
- 11:33Speaker 1: Crrol understands the AT means upload this file correct.
- 11:36Speaker 2: It handles reading the file content and sending it appropriately.
- 11:39Speaker 1: Okay, so you've send the PST request, maybe logged in.
- 11:42Speaker 1: How do you know if it actually worked.
- 11:43Speaker 2: The simplest check shown in the source is just to
- 11:46Speaker 2: look for a specific piece of text in the HTML
- 11:48Speaker 2: response that you know only appears after a successful submission.
- 11:53Speaker 1: Like log in, successful or welcome back user exactly.
- 11:57Speaker 2: You get the response page source from curl and search
- 12:00Speaker 2: that string for your success message. If it's there, it probably.
- 12:03Speaker 1: Worked, right. Okay, single pages forms, but the real power
- 12:09Speaker 1: comes from scraping lots of pages, like product listings or
- 12:12Speaker 1: search results that span multiple pages. How do you handle
- 12:16Speaker 1: that pagination?
- 12:17Speaker 2: Yeah, traversing multiple pages. You start by scraping the first
- 12:20Speaker 2: page as.
- 12:20Speaker 1: Usual, get the data, find the image, whatever, right, But.
- 12:23Speaker 2: While you're doing that, you also use XPath to look
- 12:26Speaker 2: for the link to the next page.
- 12:28Speaker 1: Like in a next button or page number link exactly.
- 12:30Speaker 2: Commonplaces are l elements with class pagination or pager. You'd
- 12:35Speaker 2: write an XPath query to find a tag inside that,
- 12:37Speaker 2: maybe specifically the one with text next, and grab its
- 12:40Speaker 2: href attribute.
- 12:41Speaker 1: So you get the URL for page two yep.
- 12:44Speaker 2: Then you scrape page two, and on page two you
- 12:47Speaker 2: look for the link to page three, and so on.
- 12:49Speaker 2: You basically collect a list of all the page URLs
- 12:51Speaker 2: you need to visit.
- 12:52Speaker 1: Make sure they're full URLs, not relative ones.
- 12:55Speaker 2: Good point. If they're relative links like page two, you
- 12:58Speaker 2: need to prepend the base website url to make them
- 13:01Speaker 2: absolute before fetching with curl.
- 13:04Speaker 1: Then you just loop through your list of URLs, scraping
- 13:07Speaker 1: each one.
- 13:07Speaker 2: Pretty much. I'll fetch page, extract data, fetch next page,
- 13:10Speaker 2: extract data, repeat.
- 13:12Speaker 1: Now this sounds like it could hit the server pretty
- 13:14Speaker 1: fast if you have hundreds of pages.
- 13:17Speaker 2: It absolutely can, and that brings up a really critical point.
- 13:20Speaker 2: The source emphasizes politeness.
- 13:22Speaker 1: Right, don't be a nuisance or worse.
- 13:24Speaker 2: Get yourself blocked. Hammering a server with rapid fire requests
- 13:28Speaker 2: is bad form and often triggers automated defenses.
- 13:30Speaker 1: So how do you be polite?
- 13:31Speaker 2: The simplest, most common way shown is to just pause
- 13:34Speaker 2: between requests. Use PHP's sleep function. The sort suggests sleep
- 13:39Speaker 2: rand one three.
- 13:40Speaker 1: Way, a random one to three seconds between fetching each page.
- 13:44Speaker 2: Yeah, it slows your script down, mimics human browsing speed
- 13:47Speaker 2: a bit more, and drastically reduces the load on their server.
- 13:51Speaker 2: It's essential for any non trivial scraping.
- 13:53Speaker 1: Okay, vital tip. So you've scraped politely across many pages,
- 13:58Speaker 1: extracted tons of data. Where does it all go? Printing
- 14:02Speaker 1: to screen is useless?
- 14:03Speaker 2: Now right, you need persistent storage. The obvious choice demonstrated
- 14:06Speaker 2: is a database. Since XMPP includes my sequel, that's the
- 14:11Speaker 2: example used.
- 14:11Speaker 1: First up is setting up the database table.
- 14:13Speaker 2: Yep, you need to design your table structure. Now define
- 14:16Speaker 2: columns that match the data points are scraping, like book title, author,
- 14:20Speaker 2: release date is SBN, etc.
- 14:22Speaker 1: And you can use food pi admin for that.
- 14:24Speaker 2: It's a handi graphical tool for creating the database and tables,
- 14:27Speaker 2: setting data types, all that stuff.
- 14:28Speaker 1: Okay, tables ready. How does the PHP script connect and
- 14:32Speaker 1: insert the scraped data?
- 14:33Speaker 2: The source uses PDOPHP data objects. It's a standard flexible
- 14:37Speaker 2: way in PHP to talk to databases, including my school.
- 14:40Speaker 2: You establish a connection using your database name.
- 14:42Speaker 1: Username, password, then insert the data.
- 14:44Speaker 2: For inserting lots of items, the best practice shown is
- 14:46Speaker 2: using prepared statements.
- 14:48Speaker 1: Why is that better than just building insert strings?
- 14:50Speaker 2: Two main reasons. Security, It prevents SEQL injection vulnerabilities, and
- 14:55Speaker 2: often better performance when you're inserting many rows with the
- 14:58Speaker 2: same structure.
- 14:59Speaker 1: How do they work.
- 15:00Speaker 2: You write the insert query once, but use placeholders like
- 15:04Speaker 2: question marks are named parameters for the actual values. You
- 15:07Speaker 2: prepare this query structure with the database okay, Then you
- 15:11Speaker 2: loop through your array of scraped data items like all
- 15:14Speaker 2: the books you found inside the loop. For each book,
- 15:18Speaker 2: you bind its specific title, author, etc. To the placeholders
- 15:22Speaker 2: in the prepared statement, and then you execute it.
- 15:24Speaker 1: So the queer structure is set once and then just
- 15:26Speaker 1: the data changes for each execution exactly.
- 15:29Speaker 2: It's cleaner and safer. One row gets inserted into your
- 15:32Speaker 2: database table for each item in your scrape data.
- 15:35Speaker 1: Array, and the source also shows getting data back out.
- 15:37Speaker 2: Yeah, completes the picture using a select query, again often
- 15:41Speaker 2: via pdo, to fetch the data you saved, perhaps looping
- 15:44Speaker 2: through the results to display them in an HTML table
- 15:47Speaker 2: on a web page.
- 15:48Speaker 1: Okay, this is getting quite sophisticated fetching, parsing, interacting, storing.
- 15:52Speaker 1: As scripts get bigger, the code could get messy, right,
- 15:55Speaker 1: repeating the same CURL setup or XPath.
- 15:59Speaker 2: Creation definitely can. That's where the source introduces object oriented
- 16:03Speaker 2: programming or OOP principles as a way to organize things better.
- 16:07Speaker 1: Making the code reusable and tidier.
- 16:10Speaker 2: That's the goal. The core idea is creating a class,
- 16:13Speaker 2: which is like a blueprint for an object. It bundles
- 16:16Speaker 2: together related data properties and functions that operate on that
- 16:19Speaker 2: data methods.
- 16:20Speaker 1: The book uses a human class analogy.
- 16:22Speaker 2: Yeah, like a human blueprint might define properties like name
- 16:25Speaker 2: and age, and methods like speak or walk. An object
- 16:29Speaker 2: is a specific instance created from that blueprint, like Bobby
- 16:32Speaker 2: eesyl new human, where Bob has his own specific name
- 16:35Speaker 2: in age.
- 16:36Speaker 1: So how does the example scrape class in the source
- 16:39Speaker 1: apply this?
- 16:40Speaker 2: It takes the common scraping tasks fetching a URL, creating
- 16:43Speaker 2: the XPath object and put some inside the class definition
- 16:46Speaker 2: as methods exactly, and it often uses a special method
- 16:49Speaker 2: called construct What's that do? The constructor runs automatically whenever
- 16:52Speaker 2: you create a new object from the class. So in
- 16:54Speaker 2: the scrape class, example, when you write page craper new
- 16:57Speaker 2: scrape http dot example dot com, the struct method immediately
- 17:01Speaker 2: takes that.
- 17:01Speaker 1: URL, the one you just passed in, calls.
- 17:03Speaker 2: An internal method maybe curl get to fetch the source
- 17:06Speaker 2: code for that specific URL, and calls another internal method
- 17:09Speaker 2: like return XPath object to create the XPath object for
- 17:13Speaker 2: that source. It does the initial setup.
- 17:14Speaker 1: Work ah, so the object is immediately ready with the
- 17:17Speaker 1: source and the XPath tool for its specific URL precisely.
- 17:21Speaker 2: The paid scraper object now holds its own source code
- 17:24Speaker 2: property and six path object property ready for you to use.
- 17:27Speaker 2: You'd access them like pagecap er, XPath object query.
- 17:30Speaker 1: And you could add more methods to the scrape class
- 17:32Speaker 1: like save image or submit form.
- 17:34Speaker 2: Absolutely you build up a reusable toolkit within the class.
- 17:38Speaker 2: Create a scrape object for any URL, and you have
- 17:40Speaker 2: all your scraping tools ready to work on that specific
- 17:43Speaker 2: pages content. Makes the main part of your script much cleaner, very.
- 17:47Speaker 1: Neat okay, final piece automation. You've built this great script,
- 17:52Speaker 1: maybe using a class it saves to a database. How
- 17:56Speaker 1: do you make it run automatically? Say every night?
- 17:58Speaker 2: Yeah, you don't want to manually run it every time.
- 18:00Speaker 2: This is where scheduling comes in. The source gives an
- 18:03Speaker 2: example using Windows task scheduler.
- 18:05Speaker 1: So it's not code within the PHP script itself.
- 18:09Speaker 2: No, Usually you leverage the operating system scheduling tools Task
- 18:12Speaker 2: scheduler on Windows, or chron jobs on Linux or Maco.
- 18:16Speaker 2: They are built for exactly this purpose.
- 18:18Speaker 1: How does it work?
- 18:19Speaker 2: Basically, you can figure a task in the scheduler. You
- 18:22Speaker 2: tell it the scheduled daily at three am for instance. Okay,
- 18:24Speaker 2: you tell the action to perform. The action is essentially
- 18:28Speaker 2: run the PHP program and tell it to execute your
- 18:30Speaker 2: specific scraping script file.
- 18:32Speaker 1: So you point it to php dot ex and then
- 18:36Speaker 1: your my scraper not PHP file exactly.
- 18:38Speaker 2: You provide the full paths. When the schedule time arrives,
- 18:41Speaker 2: the OS runs php Php execute your script and hopefully
- 18:45Speaker 2: your database gets updated with fresh data and it just.
- 18:47Speaker 1: Runs in the background without you doing anything.
- 18:49Speaker 2: That's the beauty of it. True automation. Wow.
- 18:52Speaker 1: Okay, so we've really gone from set up getting PHP EXMPP,
- 18:57Speaker 1: enabling CURL all the way through fetching pages, dealing with readerrects,
- 19:01Speaker 1: user agents.
- 19:02Speaker 2: Extracting data with XPath for structure or custom functions like
- 19:07Speaker 2: scrap between for trickier bits, handling.
- 19:09Speaker 1: Images, simulating form posts, managing cookies for logins, traversing pagization
- 19:15Speaker 1: politely with delays.
- 19:16Speaker 2: Saving the results into a my school database using PDO
- 19:19Speaker 2: and prepared statements, organizing the code using op with classes.
- 19:23Speaker 1: And finally automating the whole thing with task scheduler or coronate.
- 19:26Speaker 1: That's quite the journey.
- 19:27Speaker 2: It really covers the fundamental life cycle of a web
- 19:30Speaker 2: scripting PASK based on these source excerpts. Even though the
- 19:33Speaker 2: web is way more complex now, especially with JavaScript rendering content.
- 19:36Speaker 1: Right, that's a whole other challenge it is, but these.
- 19:38Speaker 2: Core concepts fetching HTTP content, parsing structured or semi structured data,
- 19:43Speaker 2: handling sessions, storing results, they still form the foundation. You
- 19:47Speaker 2: might add tools like headless browsers to handle JavaScript on top,
- 19:50Speaker 2: but you still need to understand these basics.
- 19:52Speaker 1: That's a great takeaway. The principles are enduring even if
- 19:55Speaker 1: the tools evolve, so thinking about these building blocks how
- 19:59Speaker 1: might you combine them for something more complex, or maybe
- 20:02Speaker 1: how does that politeness principle the sleep delay become even
- 20:05Speaker 1: more important, maybe even in ethical consideration when you think
- 20:08Speaker 1: about scraping at a really large scale, what's the most
- 20:11Speaker 1: interesting challenge you could tackle just starting with these foundational
- 20:14Speaker 1: ideas