如何拍摄多张图片链接

How to take multiple images links

def get_links(statu, data, n_img, url, agent):
    if statu==0:
        print("The website doesn't response. Please try again later",end=" ")
    else:
        img_links=[]
        r=requests.get(url,headers=agent).text
        soup=BeautifulSoup(r,"lxml")
        results=soup.find_all("div",attrs={"class":"view"})
        results=soup.find_all("div",attrs={"class":"view"})
        results=soup.find_all("div",attrs={"class":"interaction-view"})
        results=soup.find_all("div",attrs={"class":"photo-list-photo-interaction"})
        # results=soup.find_all("a",attrs={"class":"overlay"},limit=n_img)
        print(results)
        for result in results:
            link=result.get("href")
            img_links.append(link)
        return img_links

为了下载多张图片,我尝试从Flickr获取链接。为此,我编写了上面的代码,一切都很好,直到行 "results=soup.find_all("div",attrs={"class":"photo-list-photo-interaction"})"。在该行之前,我可以使用 HTML 代码。但是,在那条线上我无法得到它。 我该如何解决这个问题。谢谢!

与其使用 Beautiful Soup 进行抓取,不如使用 API instead? Alternatively, you could use Flickr's RSS Feeds 并使用 feedparser 模块解析它们。

如果你还想使用BeautifulSoup:

def flickr_photos(url):
    img_urls = []
    resp = requests.get(url)
    soup = BeautifulSoup(resp.text)

    photos = soup.find_all('div', {'class': 'view'})

    for photo in photos:
        try:
            img = photo['style'].split('(//').pop()
            if img.startswith('live'):
                img_urls.append(f'https://{img[:-1]}')
        except:
            pass
    return img_urls

您的代码不起作用的原因是因为 Flickr 在 background-image 样式属性中具有图像的 url。