SoFunction
Updated on 2025-03-05

Go combines JavaScript to capture image links in web pages

Preface

In today's digital age, data is a source of money, and for many projects and applications, it is crucial to acquire and utilize data on the Internet. One of the requirements scenarios is to grab image links from web pages, which are widely used in various projects, especially in animation image collection projects.

Demand scenarios

Project requirements for animation pictures

Suppose we are developing an anime image collection project, and we need to get links to related images from Baidu image search results. These links will be used to download images and build our image database. This requirement background can be applied to a variety of fields, from art research to entertainment information.

Advantages of Go and JavaScript

The combination of Go and JavaScript has several advantages, especially for crawling and parsing tasks of web content:

  • Concurrent Processing: Go is a powerful concurrent programming language that can easily handle multiple HTTP requests, thereby improving crawling speed.
  • JavaScript processing: JavaScript can modify the DOM (document object model) after the web page is loaded, which is very useful for grabbing links to images that are dynamically loaded through JavaScript.
  • Rich library support: Go and JavaScript have rich library and tool ecosystems that can easily solve various problems.
  • Performance and efficiency: Go is known for its efficient performance, while JavaScript is standard on the web front-end. The combination of the two can achieve ideal results in crawling tasks.

Anti-climbing response strategy

When performing network crawling, anti-crawling mechanisms are often encountered, which are designed to protect websites from illegal data collection. Here are the strategies to deal with anti-crawl mechanisms:

  • Using a proxy: Configure a proxy server to hide your real IP address and reduce the risk of being blocked. In the full crawl code, we will use the following proxy information:
  • Simulate user behavior: By setting a legitimate User-Agent header, the request looks like it was issued by a real browser, not a crawler.
  • Speed ​​limit: Avoid too frequent requests, control crawl speed by adding delays or using a timer to reduce the risk of being detected.
  • Handling verification codes and login: Some websites may require users to enter verification codes or log in to access content, and corresponding codes are required to handle these situations.

Crawling process

The crawling process can be divided into the following steps:

  • Use Go to send HTTP requests to get the HTML content of Baidu image search results page.
  • Use JavaScript to parse the page and extract image links.

Here is a detailed description of the crawling process:

Step 1: Send HTTP Request

First, we use Go to send HTTP requests to get the HTML content of Baidu image search results page. Here we use the net/http package of the Go standard library to implement it, and at the same time configure proxy information:

proxyHost := ""
proxyPort := "5445"
proxyUser := "16QMSOML"
proxyPass := "280651"

proxyUrl := ("http://%s:%s@%s:%s", proxyUser, proxyPass, proxyHost, proxyPort)
proxy := func(_ *) (*, error) {
    return (proxyUrl)
}

transport := &{
    Proxy: proxy,
}

client := &{
    Transport: transport,
}

url := "/images/search?q=anime"
resp, err := (url)
defer ()

if err != nil {
    (err)
}

body, err := ()

if err != nil {
    (err)
}

// at this time,bodyThe Baidu image search result page containsHTMLcontent

Step 2: Use JavaScript to parse the page

In this step, we use a Go library, such as /rogchap/v8go, to execute JavaScript code and parse the page. Here is a sample code snippet that demonstrates how to use JavaScript to extract image links:

ctx, _ := (nil)
_, _ = (`
    var images = ('img');
    var imageLinks = [];
    for (var i = 0; i < ; i++) {
        var src = images[i].src;
        (src);
    }
    imageLinks;
`, "")

result, _ := ("getImages();", "")
imageLinks, _ := ()

// Now,imageLinksContains links to the image extracted from the page

Summarize

Finally, by using the captured image link to download images, you can build your anime image collection project. Note that the code in this example is for demonstration purposes only and more features and improvements may be needed in the actual project.

This is the article about Go combining JavaScript to crawl image links in web pages. For more information about Go JavaScript crawling web page image links, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!