gpt4 book ai didi

python - 从 9gag 抓取图像,无法读取正确的 HTML 页面

转载 作者:行者123 更新时间:2023-12-01 07:35:29 25 4
gpt4 key购买 nike

我正在尝试编写一个脚本,该脚本只会为图像和仅图像刮掉 9gag。但我遇到了一个问题,那就是我的请求或 Beautifulsoup 得到了错误的 HTML 页面。Beautifulsoup 当前正在获取源页面,而不是包含图像的页面。
为什么 Beautifulsoup 排除包含实际图像的类?或者是不同的 HTML 页面?

我已经尝试了美丽汤“解析器”的不同格式,但仍然得到错误的页面。

如果您转到 9gag 并右键单击“检查”,您可以访问图像以及使用脚本提取图像的页面。

我的脚本:

import requests
from bs4 import BeautifulSoup
import os


def download_image(url, fileName): #save image function
path = os.path.join("imgs", fileName)
f = open(path, 'wb')
f.write(requests.get(url).content)
f.close()


def fetch_url(url): # fetching url
page = requests.get(url)
return page

def parse_html(htmlPage): #parsing the url
soup = BeautifulSoup(htmlPage, "html.parser")
return soup


def retrieve_jpg_urls(soup):

list_of_urls = soup.find_all('list') #classes wanted
parsed_urls = []
for index in range(len(list_of_urls)):
try:
parsed_urls.append(soup.find_all('img')[index].attrs['src']) #img wanted inside class
except:
next
return parsed_urls


def main():
htmlPage = fetch_url("https://9gag.com/")
soup = parse_html(htmlPage.content)
jpgUrls = retrieve_jpg_urls(soup)
for index in range(len(jpgUrls)):
try:
download_image(jpgUrls[index], "savedpic{}.jpg".format(index))
except:
print("failed to parse image with url {}".format(jpgUrls[index]))
print("")

if __name__ == "__main__":
main()

Beautifulsoup 得到了什么:

<!DOCTYPE html>

<html lang="en">
<head>
<title>9GAG: Go Fun The World</title>
<link href="https://assets-9gag-fun.9cache.com" rel="preconnect"/>
<link href="https://img-9gag-fun.9cache.com" rel="preconnect"/>
<link href="https://miscmedia-9gag-fun.9cache.com" rel="preconnect"/>
<link href="https://images-cdn.9gag.com/img/9gag-og.png" rel="image_src"/>
<link href="https://9gag.com/" rel="canonical"/>
<link href="android-app://com.ninegag.android.app/http/9gag.com/" rel="alternate"/>
<link href="https://assets-9gag-fun.9cache.com/s/fab0aa49/5aa8c9f45ee3dd77f0fdbe4812f1afcf5913a34e/static/dist/core/img/favicon.ico" rel="shortcut icon"/>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<meta content="9GAG has the best funny pics, gifs, videos, gaming, anime, manga, movie, tv, cosplay, sport, food, memes, cute, fail, wtf photos on the internet!" name="description"/>

我想要以下内容:

<img src="https://img-9gag-fun.9cache.com/photo/aLgyG2V_460s.jpg" alt="There&amp;#039;s genuine friend love there" style="min-height: 566.304px;">

最佳答案

尝试提取页面上的 JSON:

import re
import json

# ...
res = requests.get(...)
html = res.content

m = re.search('JSON\.parse\((.*)\);</script>', html)
double_encoded = m.group(1)
encoded = json.loads(double_encoded)
parsed = json.loads(encoded)

images = [p['images']['image700']['url'] for p in parsed['data']['posts']]
print(images)

输出:

['https://img-9gag-fun.9cache.com/photo/abY9Wg8_460s.jpg', 'https://img-9gag-fun.9cache.com/photo/aLgy4o5_460s.jpg', 'https://img-9gag-fun.9cache.com/photo/aE2LVeM_460s.jpg', 'https://img-9gag-fun.9cache.com/photo/amBEGb4_700b.jpg', 'https://img-9gag-fun.9cache.com/photo/aKxrv56_460s.jpg', 'https://img-9gag-fun.9cache.com/photo/a5M8wXN_460s.jpg', 'https://img-9gag-fun.9cache.com/photo/aNY6QEv_700b.jpg', 'https://img-9gag-fun.9cache.com/photo/aYY2Deq_700b.jpg', 'https://img-9gag-fun.9cache.com/photo/aQR0AEw_460s.jpg', 'https://img-9gag-fun.9cache.com/photo/aLgy19P_700b.jpg']

关于python - 从 9gag 抓取图像,无法读取正确的 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57008817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com