我试图通过这个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__()
发现夫妻错误:
首先,您需要修复网址,因为它正在尝试访问无效的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__()
另外:没有必要使用硒,因为如果你做多页,这可能会减慢过程。您可以通过仅使用请求来简化它,并且如果您将它放在.close()
语句中,则无需使用with
文件,因为它会在完成后自动关闭:
更短的代码:
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)