我试图通过这个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__()
我发现了几个错误:
首先,您需要修复url,因为它试图访问
http:////ak-static.cms.nba.com/wp-content/uploads/headshots/nba/latest/260x190/1629121.png
,这是无效的。因此,将行更改为:其次,您需要以字节的形式写入。所以改为:
代码:
另外:没有必要使用selenium,因为如果您要做多页,这可能会减慢处理过程。您可以通过使用请求来简化它,如果您将文件放入
with
语句中,还可以消除使用.close()
文件的需要,因为文件完成后将自动关闭:较短的代码:
相关问题 更多 >
编程相关推荐