Home >Web Front-end >JS Tutorial >How does jsoup save images from crawled websites locally?

How does jsoup save images from crawled websites locally?

php中世界最好的语言
php中世界最好的语言Original
2018-04-13 09:50:512504browse

This time I will show you how jsoup saves the pictures of the crawled website to the local. What are the things to note? The following is a practical case. , let’s take a look. Because

project requirements

require vehicle brand information and car series information, I spent a day yesterday studying jsoup crawling website information. The project is written using maven spring springmvc mybatis. jsoup development guide address

This is the address of the website that needs to be crawled

https://car.autohome.com.cn/zhaoche/pinpai/ 1. First add dependencies

in pom.xml Because I need to save the image locally, I added the commons-net package

<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
    <dependency>
      <groupid>org.jsoup</groupid>
      <artifactid>jsoup</artifactid>
      <version>1.10.3</version>
    </dependency>
<!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
    <dependency>
      <groupid>commons-net</groupid>
      <artifactid>commons-net</artifactid>
      <version>3.3</version>
    </dependency>

2. Implementation of crawler code

@Controller
@RequestMapping("/car/")
public class CarController {
  //图片保存路径
  private static final String saveImgPath="C://imgs";
  /**
  * @Title: insert 品牌名称 和图片爬取和添加
  * @Description: 
  * @param @throws IOException  
  * @return void  
  * @throws
  * @date 2018年1月29日 下午4:42:57
  */ 
  @RequestMapping("add")
  public void insert() throws IOException {
    //定义想要爬取数据的地址
    String url = "https://car.autohome.com.cn/zhaoche/pinpai/";
    //获取网页文本
    Document doc = Jsoup.connect(url).get();
    //根据类名获取文本内容
    Elements elementsByClass = doc.getElementsByClass("uibox-con");
    //遍历类的集合
    for (Element element : elementsByClass) {
      //获取类的子标签数量
      int childNodeSize_1 = element.childNodeSize();
      //循环获取子标签内的内容
      for (int i = 0; i <p style="text-align: left;"> I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website! </p><p>Recommended reading: </p><p></p>JS prompt text box email address completion<p><a href="http://www.php.cn/js-tutorial-392380.html" target="_blank"></a><br></p>getBoundingClientRect usage and compatibility handling<p><a href="http://www.php.cn/js-tutorial-392375.html" target="_blank"></a><br></p>

The above is the detailed content of How does jsoup save images from crawled websites locally?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn