Q
用機械化下載文件
4
A
回答
1
可能不是你要找的答案,但我已經使用LXML並請求庫一起自動錨取:
相關LXML例子http://lxml.de/lxmlhtml.html#examples(與要求更換的urllib)
而請求庫主頁http://docs.python-requests.org/en/latest/index.html
它不像機械化一樣緊湊,但提供更多的控制。
3
import urllib, urllib2,cookielib, re
#http://www.crummy.com/software/BeautifulSoup/ - required
from BeautifulSoup import BeautifulSoup
HOST = 'https://www.adobe.com/'
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
req = opener.open(HOST + 'pdf')
responce = req.read()
soup = BeautifulSoup(responce)
pdfs = soup.findAll(name = 'a', attrs = { 'href': re.compile('\.pdf') })
for pdf in pdfs:
if 'https://' not in pdf['href']:
url = HOST + pdf['href']
else:
url = pdf['href']
try:
#http://docs.python.org/library/urllib.html#urllib.urlretrieve
urllib.urlretrieve(url)
except Exception, e:
print 'cannot obtain url %s' % (url,)
print 'from href %s' % (pdf['href'],)
print e
else:
print 'downloaded file'
print url
相關問題
- 1. 下載PDF文件是使用紅寶石機械化
- 2. 使用機械化下載多線程文件?
- 3. Python下載文件時沒有機械化的直接鏈接
- 4. 使用機械化
- 5. 保存紅寶石文件機械化
- 6. 機械化Javascript
- 7. 在機械化
- 8. 與機械化
- 9. 紅寶石機械化:如何讀取下載的二進制CSV文件
- 10. 如何保存通過按鈕點擊機械化下載的文件
- 11. 機械化重定向/引入nokogiri(使用機械化小白)
- 12. 機械化再次提交下一頁
- 13. 用Ruby解析機械化
- 14. 使用機械化與Python
- 15. 使用Python機械化
- 16. 無法使用機械化
- 17. 使用WWW ::機械化
- 18. 用機械化抓取way2sms
- 19. NoMethodError從機械化
- 20. 機械化jQuery中
- 21. Symfony和機械化
- 22. 機械化提交
- 23. WWW ::機械化Perl
- 24. 如何在載波圖像下載期間設置cookie /使用機械化
- 25. WWW ::機械化文本字段問題
- 26. Python - 機械化輸入文本形式
- 27. Python機械化:用文件(圖像)提交表單?
- 28. 如何上傳文件,使用Python機械化,一擰:)
- 29. 使用perl WWW ::對本地文件進行機械化
- 30. 使用機械化上傳文件失敗
嗨大衛,我打算試一試吧 – Dave