Python3,通过单击按钮从url下载文件
我需要从这样的链接下载文件Python3,通过单击按钮从url下载文件,python,parsing,web-scraping,download,urllib,Python,Parsing,Web Scraping,Download,Urllib,我需要从这样的链接下载文件 但我不能使用urllib.request或requests库,因为它下载的是html,而不是midi。有什么解决办法吗?这里还有按钮本身的链接,通过添加适当的标题并使用会话,我们可以使用请求模块下载并保存文件 import requests headers = { "Host": "freemidi.org", "Connection": "keep-alive", "User-Agent":
但我不能使用
urllib.request
或requests
库,因为它下载的是html,而不是midi。有什么解决办法吗?这里还有按钮本身的链接,通过添加适当的标题并使用会话,我们可以使用请求模块下载并保存文件
import requests
headers = {
"Host": "freemidi.org",
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.9",
}
session = requests.Session()
#the website sets the cookies first
req1 = session.get("https://freemidi.org/getter-13560", headers = headers)
#Request again to download
req2 = session.get("https://freemidi.org/getter-13560", headers = headers)
print(len(req2.text)) # This is the size of the mdi file
with open("testFile.mid", "wb") as saveMidi:
saveMidi.write(req2.content)
非常感谢。它终于起作用了。但需要一些改变。键入req2.content而不是req2.text,选择“wb”模式和“.mid”,而不是“.midi*”