用Python下载.txt,通过php请求访问


Download .txt with Python that is accessed through an php request

我正在尝试自动下载这些电子书:http://www.getfreeebooks.com/?p=10394

使用这样的url可以下载单个。txt文件:http://manybooks.net/_scripts/send.php?tid=lovecrafthother06At_the_Mountains_of_Madness&书= 1:文本:. txt文本

但是文件不能保存,例如:

url = "http://manybooks.net/_scripts/send.php?tid=lovecrafthother06At_the_Mountains_of_Madness&book=1:text:.txt:text"
testfile = urllib.URLopener()
testfile.retrieve(url, "book 001.txt")

可能是因为文件是通过php访问的,我试图下载一个php请求。然而,我似乎找不到正确的方法。

谢谢你调查我的问题!Jelle

这不是因为文件是通过php访问的,而是因为服务器似乎不喜欢您使用空的User-agent。去吧,让他们知道你是谁!

headers = {
    "User-Agent": "jpostma1's crawler"
}
url = "http://manybooks.net/_scripts/send.php?tid=lovecrafthother06At_the_Mountains_of_Madness&book=1:text:.txt:text"
req = urllib2.Request(url, headers=headers)
testfile = urllib2.urlopen(req)
with open("book 001.txt",'w') as w:
    w.write(testfile.read())