我正在尝试从有登录的网站下载大约20个左右的pdf。这是我到目前为止,但它无法下载任何有效的pdf(即它们都已损坏)。我也是python的新手。
import mechanize
import urllib2
def download_file(download_url):
response = urllib2.urlopen(download_url)
print response.geturl()
print response.read()
file = open("document.pdf", 'wb')
file.write(response.read())
file.close()
brwser = mechanize.Browser()
brwser.addheaders = [('User-agent', 'Firefox')]
response = brwser.open(url)
brwser.select_form(nr = 0)
brwser.form['UserName'] = 'username'
brwser.form['Password'] = 'password'
nextpage = brwser.submit()
# Navigate to the page I want
for link in brwser.links():
if link.text == 'Some pdf':
request = brwser.follow_link(link)
download_file(link.url)
我不知道该尝试什么。 pdf的URL就像这样
https://example.com/something/source2.aspx?id=e9a9bfdc-7d97-e411-9e03-76439cf4d30e
response.read()如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
Source
</title>
<script type='text/javascript'>
window.onload = function () {
var url = window.location.href.replace('source.aspx?', 'source2.aspx?');
window.location = url;
};
</script>
</head>
<body>
<div style='position:fixed; height:100%; width:100%; overflow:hidden; top:100px; left:100px;'>Loading, please wait.</div>
</body>
</html>
那么如何下载这些文件?