【发布时间】:2022-08-19 16:08:05
【问题描述】:
我想从网站爬取和下载图片,但不知道为什么在运行此代码时收到错误消息。
import requests
from bs4 import BeautifulSoup
from urllib.request import urlretrieve
url = \'https://www.thsrc.com.tw/tw/TimeTable/SearchResult\'
response = requests.get(url)
response.encoding = \'utf-8\'
soup = BeautifulSoup(response.text, \'html.parser\')
all_imgs = soup.find_all(\'img\')
for index, img in enumerate(all_imgs):
if index!=0:
print(img[\'src\'])
image_path = \'https://www.thsrc.com.tw\'+img[\'src\']
image_name = img[\'src\'].split(\'/\')[-1]
print(\'image path is {}, file name is {}\'.format(image_path, image_name))
urlretrieve(image_path, \'save_image/\'+image_name)
这就是我收到的:
标签: python image web-scraping beautifulsoup