我正在写一个来自《自动化无聊的东西》一书的项目。任务如下:
图像站点下载程序
编写一个程序,进入Flickr或Imgur等照片共享网站, 搜索一类照片,然后下载所有结果 图像。您可以编写一个程序,用于任何具有 搜索功能
这是我的密码:
import requests, bs4, os
# The outerHTML file which I got by rightClicking and copying the <html> tag on 'page source'
flickrFile=open('flickrHtml.html',encoding="utf8")
#Parsing the HTML document
flickrSoup=bs4.BeautifulSoup(flickrFile,'html.parser')
# categoryElem is the Element which has image source inside
categoryElem=flickrSoup.select("a[class='overlay']")
#len(categoryElem)=849
os.makedirs('FlickrImages', exist_ok=True)
for i in range(len(categoryElem)-1):
# Regex searching for the href
import re
html=str(categoryElem[i])
htmlRegex=re.compile(r'href.*/"')
mo=htmlRegex.search(html)
imageUrl=mo.group()
imageUrl=imageUrl.replace('"','')
imageUrl=imageUrl.replace('href=','')
imageUrlFlickr="https://www.flickr.com"+str(imageUrl)
# Downloading the response object of the Image URL
res = requests.get(imageUrlFlickr)
imageSoup=bs4.BeautifulSoup(res.text)
picElem=imageSoup.select('div[class="view photo-well-media-scrappy-view requiredToShowOnServer"] img')
# Regex searching for the jpg file in the picElem HTML element
html=str(picElem)
htmlRegex=re.compile(r'//live.*\.jpg')
mo=htmlRegex.search(html)
try:
imageUrlRegex=mo.group()
except Exception as exc:
print('There was a problem: %s' % (exc))
res1=requests.get('https:'+imageUrlRegex)
try:
res1.raise_for_status()
except Exception as exc:
print('There was a problem: %s' % (exc))
# Dowloading the jpg to my folder
imageFile = open(os.path.join('FlickrImages', os.path.basename(imageUrlRegex)), 'wb')
for chunk in res1.iter_content(100000):
imageFile.write(chunk)
在查找了this question之后,我估计为了下载图片“Sea”的所有400万个结果,我复制了整个OuterHTML(如回答问题时所述)。如果我没有看这个问题,也没有复制完整的HTML源代码(在我的代码中,它存储在flickrFile=open('flickrHtml.html',encoding="utf8")
),我最终会得到categoryElem
等于24,因此只下载24张图片,而不是849张图片
There are 4 million pictures, how do I download all of them, without having to download the HTML source to a separate file?
我正在考虑我的计划,以实现以下目标:
我没有使用第一种方法,因为我不知道如何获得第一张图片的链接。我试图获取它的URL,但当我从“照片流”中检查第一张图片(或任何其他图片)的元素时,它给了我一个指向特定用户的“照片流”的链接,而不是一般的“海上搜索照片流”
Here is the link for the photo stream Search
如果有人也能帮我,那就太好了
Here is some code来自完成相同任务的人,但他只下载了前24张图片,这些图片显示在原始的、未呈现的HTML上
如果要使用
requests + Beautfulsoup
,请尝试以下操作(通过传递参数page
):如果使用selenium,可能会更简单,示例代码如下:
并在我的电脑上成功下载
首先,从Flicker这样的网站上抓取400万条搜索结果可能是不道德的。网络爬虫应该尽最大努力通过最小化服务器上的负载来尊重他们正在从中爬虫的网站。在短时间内有400万个请求可能会使你的IP被禁止。如果你使用代理,你可以绕过这一点,但再次-高度不道德。你也会遇到版权问题的风险,因为flicker上的很多图像都是受版权保护的
如果你要继续这样做,你就必须使用Scrapy和Scrapy Selenium组合。Scrapy非常适合运行并发请求,这意味着您可以同时请求大量图像。您可以在此处了解有关Scrapy的更多信息:https://docs.scrapy.org/en/latest/
工作流程如下所示:
这就是Scrapy所需要的,但我强烈建议不要尝试刮取400万个元素。您可能会发现,您遇到的性能问题不值得您花费时间,特别是因为这应该是一次学习经历,您可能永远不需要刮取那么多元素
相关问题 更多 >
编程相关推荐