【问题标题】:How to take multiple images links如何拍摄多张图片链接
【发布时间】:2020-06-12 15:49:05
【问题描述】:
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 代码。但是,在那条线上我无法得到它。 我该如何解决这个问题。谢谢!

【问题讨论】:

  • 你可以使用 flickr api 代替

标签: python beautifulsoup request python-requests urllib


【解决方案1】:

与其用 Beautiful Soup 刮,为什么不使用 API 代替呢?或者,您可以使用 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。

【讨论】:

  • 非常感谢。实际上,我使用不同的网站来开发工具包。不仅是 Flickr。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多