Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use R language to capture web page pictures

Shulou Source: shulou.com Published: 2022-06-01 22:19:34 11月12日 Update

This article shows you how to use R language to capture web page pictures, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

What I want to climb today is a multi-map Zhihu web page, which is an external shooting post, which introduces a lot of external shooting skills, very practical practical information.

Library (rvest)

Library (downloader)

Library (stringr)

Library (dplyr)

Https://www.zhihu.com/question/19647535

After opening the page, locate any picture in the post content, then right-click-check the element (Ctrl+Shift+I), the page structure that pops up on the right side of the page will automatically navigate to the address of the picture, and you will see the name label of the picture in the html structure:-- (img); address label-- (src).

What we want to get is the address information of the image. You can try to download a single image using the downlond function.

Url%html_attr ("src")

What we need to get is the src content (that is, the image address) under the img tag in the div branch structure where the picture is located. Then if you don't want to capture a lot of irrelevant images, you must know where the target image is stored. The above code process locates the url (the URL of the post page) to the div branch structure where the target image is located. Then locate the src information (that is, the target image URL) in the img (picture tag) in the branch structure.

Run the above two lines of code and preview the first few lines of the link vector with the head function to see if the image address you got is correct.

Unfortunately, in the string vector that we obtained to store the image address information, there is an invalid URL on every other line. If we do not clear these invalid URLs or filter out those complete URLs, the download function executes to the invalid URL will be terminated, and the download process will fail.

Here you need to use the stringr package for conditional filtering.

Pat = "https"

Link

Tags: Picture structure address location web page URL function information target branch content label that is process that is code location interval name vector Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MariaDB macOS Shulou Technology Huawei