将图像抓取到 Excel 文件中 - Selenium/Pandas/Python

问题描述 投票:0回答:1

我希望将网站上的图像抓取到 Excel 文件中。

下面是我的代码片段。这会获取 SRC url 并将其放入 Excel 工作表中。可以把它变成实际图像吗?

if len(driver.find_elements(By.CSS_SELECTOR, '#FMP-target'))>0:
    image = driver.find_element(By.CSS_SELECTOR, '#FMP-target').get_attribute('src')
    print(image)
    images.append(image)
else:
    photo = "No photo"
    print(photo)
    images.append(photo)
driver.quit()

df = pd.DataFrame(zip(images,house_name,description_details,house_price,specs,ratings,cancellation_policy,urls),columns=['Photo','Property Name','Description','Price','Specifications','Ratings','Cancellation Policy','Link'])
df.to_excel(r{file},index=False)

以下是我正在测试的网址

https://www.airbnb.com.au/rooms/50961691?adults=9&children=2&pets=1&search_mode=regular_search&check_in=2024-12-22&check_out=2024-12-28&source_impression_id=p3_1720759519_P3FseNFXiyBEU7hU&pre previous_page_section_name=1000&federated_search_id=7f29c222-115b-444d- bcdd-895c3e3151b4

谢谢!

python excel pandas selenium-webdriver
1个回答
0
投票

从网站下载图像并插入到工作表锚点 A1 的示例;

import requests
from PIL import Image as Image
from openpyxl import Workbook
import io
from openpyxl.drawing.image import Image as opImage


url = "https://a0.muscache.com/im/pictures/miso/Hosting-50961691/original/3163e6cb-692f-40c8-80f0-c6a4539d1448.jpeg?im_w=1200"
response = requests.get(url)

img_data = response.content
img_png = Image.open(io.BytesIO(requests.get(url).content)).convert("RGB")

wb = Workbook()
ws = wb.active

# insert image
img_stream = io.BytesIO()
img_png.save(img_stream, format='PNG')
img_openpyxl = opImage(img_stream)

ws.add_image(img_openpyxl, 'A1')

wb.save("image.xlsx")
© www.soinside.com 2019 - 2024. All rights reserved.