我使用 Python 中的 Bs4 从 nmgncp.com 下载壁纸。 然而,代码仅下载 16KB 的文件,而完整图像约为 300KB。 请帮我。我什至尝试过 wget.download 方法。
PS:- 我在 Windows 10 上使用 Python 3.6。
这是我的代码::--
from bs4 import BeautifulSoup
import requests
import datetime
import time
import re
import wget
import os
url='http://www.nmgncp.com/dark-wallpaper-1920x1080.html'
html=requests.get(url)
soup=BeautifulSoup(html.text,"lxml")
a = soup.findAll('img')[0].get('src')
newurl='http://www.nmgncp.com/'+a
print(newurl)
response = requests.get(newurl)
if response.status_code == 200:
with open("C:/Users/KD/Desktop/Python_practice/newwww.jpg", 'wb') as f:
f.write(response.content)
最佳答案
问题的根源是因为有一个保护:图像页面需要一个引用,否则它会重定向到html页面。
源代码已修复:
from bs4 import BeautifulSoup
import requests
import datetime
import time
import re
import wget
import os
url='http://www.nmgncp.com/dark-wallpaper-1920x1080.html'
html=requests.get(url)
soup=BeautifulSoup(html.text,"lxml")
a = soup.findAll('img')[0].get('src')
newurl='http://www.nmgncp.com'+a
print(newurl)
response = requests.get(newurl, headers={'referer': newurl})
if response.status_code == 200:
with open("C:/Users/KD/Desktop/Python_practice/newwww.jpg", 'wb') as f:
f.write(response.content)
关于python - 无法在 Python 中下载完整文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46547676/