为什么HTML只能获得6个图像链接?

2024-04-28 23:47:31 发布

您现在位置:Python中文网/ 问答频道 /正文

晚上好

大家好,所以我正试图从这个网站上抓取图片

https://unsplash.com/t/wallpapers

是的,我知道他们有一个API,但我想先使用我的编码技能,然后再使用API

这是我的代码:

from requests_html import HTMLSession

session = HTMLSession()
url ="https://unsplash.com/t/wallpapers"

r = session.get(url)
r.html.render(sleep=3)


images = r.html.find("._2UpQX")

imglinks =[]

for image in images:
 imglinks.append(image.attrs["src"])
 
imglinks

print(imglinks)

我只能得到6个图片链接:(

这是输出的图像,也是该网站的css

输出:Output

网站的Css:CSS of website


Tags: httpsimagecomapiurl网站sessionhtml
1条回答
网友
1楼 · 发布于 2024-04-28 23:47:31

我访问了website并注意到它将只渲染屏幕中的图像,即当您滚动时,上面的图像将不再渲染,而新图像将被渲染。图像数量也将根据屏幕大小而变化

我试图搜索如何发送屏幕大小,以便我们可以发送更大的屏幕大小,但我找不到任何方法来这样做

但我还有一个想法,我们可以在每次扫描图像时保持滚动

它起作用了!我得到了23张运行以下脚本的图像(实际上每次运行都会有所不同,即使我不知道为什么)

from requests_html import HTMLSession

max_levels = 10
scroll_increment = 10
imglinks = set()

session = HTMLSession()
url = "https://unsplash.com/t/wallpapers"

scroll = 0

for level in range(max_levels):
    print('level', level, 'scroll', scroll)
    r = session.get(url)
    r.html.render(scrolldown=scroll)
    scroll += scroll_increment

    images = r.html.find("._2UpQX")
    print('new images found', len(images))

    for image in images:
        imglinks.add(image.attrs["src"])
    print('unique images found till now', len(imglinks))

session.close()

print(imglinks)
print(len(imglinks))

我将留给你去探索卷轴的长度,不需要卷轴

我没有试过,但它也可能对你有帮助

相关问题 更多 >