python-3.x - Beautifulsoup 图片下载错误
问题描述
我正在尝试从 Beautifulsoup 刮取的图像网址下载图像。在阅读了其他一些示例后,我试图让这段代码工作
得到错误:
f.write(requests.get(img))
TypeError: a bytes-like object is required, not 'Response
line:f.write(requests.get(img)[What goes here?])
现在给我添麻烦了。我用soup = BeautifulSoup(source, 'html.parser')
作为参考使用的地方soup = BeautifulSoup(r.content)
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
import urllib.request
from bs4 import BeautifulSoup
import codecs
import sys
import requests
from os.path import basename
lines = open('list.txt').read().splitlines()
for designers in lines:
for i in range(1):
url = 'https://www.example.com/Listings?st=' + author + '{}'.format(i)
source = urllib.request.urlopen(url)
soup = BeautifulSoup(source, 'html.parser')
for products in soup.find_all('li', class_='widget'):
image = products.find('img', class_='lazy-load')
print(image.get('data-src'))
img = (image.get('data-src'))
with open(basename(img), "wb") as f:
f.write(requests.get(img)**[What goes here?]**)
谢谢!