我正在尝试使用python的requests modu从web下载并保存一个图像

2024-09-30 00:24:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过这个url的请求下载这个图像,但是idk在第17行发生了一个错误,没有定义问题所在

我尝试过在url中添加http://以使其成为一个清晰的url

这是我写的代码

from selenium import webdriver
from bs4 import BeautifulSoup
import requests
import os
driver = webdriver.Chrome(executable_path= r'E:/Summer/FirstThings/Web scraping (bucky + pdf)/webscraping/tutorials-master/chromedriver.exe')
url = 'https://www.nba.com/players/jaylen/adams/1629121'
driver.get(url)
#print(driver.page_source)

soup = BeautifulSoup(driver.page_source , 'lxml')
div = soup.find('section' , class_='nba-player-header__item nba-player-header__headshot')
img = div.find('img')
print("")
m=('http://'+ img['src'])

f = open('jaylen_adams.jpg','w')
f.write(requests.get(m).content)
f.close()

driver.__exit__()

Tags: fromimporthttpurlimggetdriverpage
1条回答
网友
1楼 · 发布于 2024-09-30 00:24:49

我发现了几个错误:

首先,您需要修复url,因为它试图访问http:////ak-static.cms.nba.com/wp-content/uploads/headshots/nba/latest/260x190/1629121.png,这是无效的。因此,将行更改为:

m=('http:'+ img['src'])

其次,您需要以字节的形式写入。所以改为:

f = open('C:/jaylen_adams.jpg','wb')

代码:

from selenium import webdriver
from bs4 import BeautifulSoup
import requests
import os
driver = webdriver.Chrome('C:/chromedriver_win32/chromedriver.exe')
url = 'https://www.nba.com/players/jaylen/adams/1629121'
driver.get(url)
#print(driver.page_source)

soup = BeautifulSoup(driver.page_source , 'lxml')
div = soup.find('section' , class_='nba-player-header__item nba-player-header__headshot')
img = div.find('img')
print("")
m=('http:'+ img['src'])  # <  - edit made here

f = open('C:/jaylen_adams.jpg','wb')   # <   edit made here
f.write(requests.get(m).content)
f.close()

driver.__exit__()

另外:没有必要使用selenium,因为如果您要做多页,这可能会减慢处理过程。您可以通过使用请求来简化它,如果您将文件放入with语句中,还可以消除使用.close()文件的需要,因为文件完成后将自动关闭:

较短的代码:

from bs4 import BeautifulSoup
import requests

url = 'https://www.nba.com/players/jaylen/adams/1629121'
response = requests.get(url)

soup = BeautifulSoup(response.text , 'lxml')
div = soup.find('section' , class_='nba-player-header__item nba-player-header__headshot')
img = div.find('img')
print("")
m=('http:'+ img['src'])

with open('C:/jaylen_adams.jpg','wb') as f:
    f.write(requests.get(m).content)

相关问题 更多 >

    热门问题