[英]How can I scrape all the images from a website?
我有一個網站,我想從該網站獲取所有圖像。
該網站本質上是一個動態的網站,我嘗試使用Google的Agenty Chrome擴展程序並遵循以下步驟:
這應該為我提供結果,但不是,它返回空輸出。
有沒有更好的選擇? BS4會為此提供更好的選擇嗎? 任何幫助表示贊賞。
我假設您要下載網站上的所有圖像。 實際上,使用漂亮的湯4(BS4)有效地做到這一點非常容易。
#code to find all images in a given webpage
from bs4 import BeautifulSoup
import urllib.request
import requests
import shutil
url=('https://www.mcmaster.com/')
html_page = urllib.request.urlopen(url)
soup = BeautifulSoup(html_page, features="lxml")
for img in soup.findAll('img'):
assa=(img.get('src'))
new_image=(url+assa)
您還可以將粘貼后的圖像下載到最后:
response = requests.get(my_url, stream=True)
with open('Mypic.bmp', 'wb') as file:
shutil.copyfileobj(response.raw, file)
兩行內容:
from bs4 import BeautifulSoup; import urllib.request; from urllib.request import urlretrieve
for img in (BeautifulSoup((urllib.request.urlopen("https://apod.nasa.gov/apod/astropix.html")), features="lxml")).findAll('img'): assa=(img.get('src')); urlretrieve(("https://apod.nasa.gov/apod/"+assa), "Mypic.bmp")
新圖像應與python文件位於同一目錄中,但可以通過以下方式移動:
os.rename()
對於McMaster網站,圖像的鏈接不同,因此上述方法無效。 以下代碼應獲取網站上的大多數圖像:
from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
import re
import urllib.request
import shutil
import requests
req = Request("https://www.mcmaster.com/")
html_page = urlopen(req)
soup = BeautifulSoup(html_page, "lxml")
links = []
for link in soup.findAll('link'):
links.append(link.get('href'))
print(links)
更新:我從一些github發布發現下面的代碼是更准確的:
import requests
import re
image_link_home=("https://images1.mcmaster.com/init/gfx/home/.*[0-9]")
html_page = requests.get(('https://www.mcmaster.com/'),headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36'}).text
for item in re.findall(image_link_home,html_page):
if str(item).startswith('http') and len(item) < 150:
print(item.strip())
else:
for elements in item.split('background-image:url('):
for item in re.findall(image_link_home,elements):
print((str(item).split('")')[0]).strip())
希望這可以幫助!
您應該使用scrapy ,通過選擇要使用css標簽下載的內容,它可以使爬網變得無縫,您可以輕松地自動進行爬網。
該網站使用CSS嵌入來存儲圖像。 如果您檢查源代碼,則可以找到具有https://images1.mcmaster.com/init/gfx/home/的鏈接,這些是實際的圖像,但實際上是縫合在一起的(圖像行)
示例: https : //images1.mcmaster.com/init/gfx/home/Fastening-and-Joining-Fasteners-sprite-60.png?ver=1539608820
import requests
import re
url=('https://www.mcmaster.com/')
image_urls = []
html_page = requests.get(url,headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36'}).text
for values in re.findall('https://images1.mcmaster.com/init/gfx/home/.*[0-9]',html_page):
if str(values).startswith('http') and len(values) < 150:
image_urls.append(values.strip())
else:
for elements in values.split('background-image:url('):
for urls in re.findall('https://images1.mcmaster.com/init/gfx/home/.*[0-9]',elements):
urls = str(urls).split('")')[0]
image_urls.append(urls.strip())
print(len(image_urls))
print(image_urls)
注意:報廢網站受版權保護
您可以使用Agenty Web抓取工具 。
src
屬性 這是回答了Agenty論壇類似的問題- https://forum.agenty.com/t/can-i-extract-images-from-website/24
完全披露 -我在Agenty工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.