【问题标题】:How to crawl pictures via python beautiful soup如何通过python美汤爬取图片
【发布时间】: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


    【解决方案1】:

    由于某些原因,img['src'] 中有一个空格,所以你必须strip() 它:

    image_path = 'https://www.thsrc.com.tw'+img['src'].strip()
    
    例子
    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)
    
    for img in soup.find_all('img'):
        print(img['src'])
        image_path = 'https://www.thsrc.com.tw'+img['src'].strip()
        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)
    

    【讨论】:

      猜你喜欢
      • 2017-09-11
      • 2016-01-07
      • 2020-09-11
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      • 2021-12-15
      • 1970-01-01
      • 2012-08-01
      相关资源
      最近更新 更多