问题
I cannot get correct img src. This is HTML I want to get.This image is data scheme URI.
<img class="rg_i Q4LuWd tx8vtf" src="data:image/jpeg;base64,9j/4AAQSkZJR ~~~ TOO LONG ~~~/Z" data-deferred="1" jsname="Q4LuWd" alt="大阪の保護猫カフェ - SAVE CAT CAFE" data-iml="610.9050000086427" data-atf="true">
And, This is my code.
val url = "https://www.google.com/search?q=cat&sxsrf=ALeKk01jWgnZ1Jwok_XfrhRYTdkwZecETg:1587538774281&source=lnms&tbm=isch&sa=X&ved=2ahUKEwiy3dTluvvoAhUPyosBHQtMAP8Q_AUoAXoECA8QAw&biw=1280&bih=616"
Jsoup.connect(url).get().select("img")
Then its result below.
<img class="rg_i Q4LuWd tx8vtf" src="data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" ~~same above~~/>
"data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" is 1px × 1px gif image. It is different image src when I get html by jsoup. Correct type of image is jpeg, but its result is gif. I can get another tag as a tag. So I think my code is not bad. Is there any settings in getting img src by Jsoup. How to get same type image src.
回答1:
It's a bit tricky, since the data you are looking for is inside script
tags, so you'll have to iterate over all the script
tags.
Next You'll have to check each element if it contains the text jpeg;base64
since jpeg
appears in some other elements that do not contain the base64 data.
I've used java and eclipse and I've had some issues with the output (it's too long for the ide and it is escaped, so /
appears as \/
, but I'm sure you'll be able to fix it:
Document doc = Jsoup.connect(url).
userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0").get();
Elements images = doc.select("script");
for (Element image : images) {
if (image.html().contains("jpeg;base64")) {
System.out.println(image.html());
System.out.println("--------------");
}
}
One last thing - add your userAgent
string to the request, else you might get a different response from the server.
回答2:
Try this
Jsoup.connect(url).get().select("img [src]")
来源:https://stackoverflow.com/questions/61360484/android-jsoup-why-i-cannot-get-correct-src-of-img