我希望将网站上的图像抓取到 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)
以下是我正在测试的网址
谢谢!
从网站下载图像并插入到工作表锚点 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")