2017-01-11 6 views
-4

タスクは簡単です:私は、Pythonのちょうど初心者ですaspxのWebページからすべてのPDFをダウンロードするにはどうしたらいいですか?

https://www.electroimpact.com/Company/Patents.aspx

:からすべてのPDFファイルをダウンロードするためにはPythonを使用しています。私はPythonのクローラを読んでいるが、サンプルはHTMLではなくaspxを扱う。そして、私は空白のファイルをダウンロードしているだけです。続き

は私のコードです:

import urllib2 
import re 

def saveFile(url, fileName): 
    request = urllib2.Request(url) 
    response = urllib2.urlopen(request) 
    with open(fileName,'wb') as handle: 
     handle.write(response.read()) 

def main(): 
    base_url = 'https://www.electroimpact.com/Company/Patents/' 
    page = 'https://www.electroimpact.com/Company/Patents.aspx' 
    request = urllib2.Request(page) 
    response = urllib2.urlopen(request) 
    url_lst = re.findall('href.*(US.*\.pdf)', response.read()) 
    print url_lst 

Result: 
    ['US5201205.pdf', 'US5279024.pdf', 'US5339598.pdf', 'US9021688B2.pdf'] 

のみ4 PDFは私の正規表現によって発見されました。実際、抽出するPDFはもっとたくさんあります。どうして?

あなたがリンクされているすべての特許文書のパスを取得します lxml.htmlcssselectの代わり re
+0

ASPXは、PHPのようなだけ異なるファイル拡張子だ、HTMLです。 – Steve

+0

ヒントのおかげで。私はurllib2 + reを使ってこの作業を解決しようとしています。しかし、私の正規表現には何か問題があるはずです。多くの項目がありません。あなたはエラーがどこにあるのか見つけるのを助けることができますか? – user7405020

+0

残念ながら、私はPythonのプログラマーではありません。 – Steve

答えて

0

#!/usr/bin/env python 
# coding: utf8 
from __future__ import absolute_import, division, print_function 
import urllib2 
from lxml import html 


def main(): 
    url = 'https://www.electroimpact.com/Company/Patents.aspx' 
    source = urllib2.urlopen(url).read() 
    document = html.fromstring(source) 
    patent_paths = [ 
     a.attrib['href'] for a in document.cssselect('div.PatentNumber a') 
    ] 
    print(patent_paths) 


if __name__ == '__main__': 
    main() 
関連する問題