【问题标题】:Why is requests_HTML only able to get 6 image links?为什么 requests_HTML 只能获取 6 个图片链接?
【发布时间】:2021-05-02 10:37:34
【问题描述】:

晚上好

大家好,我想从这个网站上抓取图片

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

【问题讨论】:

    标签: python css web-scraping xpath python-requests-html


    【解决方案1】:

    我访问了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))
    

    我会留给你探索卷轴长度,不需要卷轴。

    我没有尝试How to Crawl Infinite Scrolling Pages using Python,但它也可能对你有所帮助

    【讨论】:

    • 谢谢,我有超过 7 个链接,但会尝试获得更多 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 2021-10-22
    • 2017-01-11
    • 2012-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多