Here's how to test web scraping code in the browser

NulledCode

Elite Member
Jr. VIP
Joined
Jun 10, 2010
Messages
2,400
Reaction score
1,676
Here's a handy way to test scraping content from a web page.

For example, let's say we want to grab the titles and links to all of the threads on the BHW homepage.
  1. Open Chrome browser.
  2. Go to https://www.blackhatworld.com/
  3. Open your dev tools, by pressing F12.
  4. Click on the "Sources" tab.
  5. Click on "+ New Snippet".
  6. Enter your javascript code.
  7. Press the "> Ctrl+Enter" button below
You can use this code snippet to inject jQuery into the website if jQuery is not available. Some websites, like BHW, have a Content-Security-Policy in place that prevents you from doing this so if you encounter websites that block you loading jQuery you can always use this extension to add it to any website.

JavaScript:
var jq = document.createElement('script');
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);

Once you have jQuery loaded, then you can write your javascript code that performs your web scraping or anything else you need to test. Enter the following code snippet and execute it as in step 7 and you will get a list of all thread titles and their links.

JavaScript:
$(".structItem-title").each(async (index, element) => {
    const title = $(element).find('a').text();
    const href = $(element).find('a').attr('href');

    console.log(`${title} -> ${href}`);
});

What's great about this approach is you don't need to constantly run your scripts locally just to make sure your web scraping code is correct. Plus, you can build a nice collection of snippets stored in your browser for reuse because chances are you will find some sites structured similarly.

Screenshot from 2022-02-18 08-21-49.png
 
Last edited:
Don’t forget some sites block jQuery injection with CSP. Using a browser extension to bypass that is handy.
 
Interesting approach but what happens after page refresh?

But interesting concept, thus can be injected via server side, and doesn't even require j query anyway.
 
Back
Top