【问题标题】:I'm trying to download and save an image from the web using python's requests module我正在尝试使用 python 的请求模块从网络下载并保存图像
【发布时间】:2019-08-30 17:28:17
【问题描述】:

我正在尝试通过此 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__()

【问题讨论】:

  • 请附上完整的错误信息。

标签: python selenium-webdriver web-scraping beautifulsoup python-requests


【解决方案1】:

我发现的几个错误:

首先,您需要修复 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)

【讨论】:

    猜你喜欢
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    相关资源
    最近更新 更多