2017-12-02 9 views
0

私はプログラミングに慣れていないし、ウェブサイトから画像やPDFをダウンロードしようとしている。ソースコードでは、必要な項目は部分的なURLを持つオプションタグにあります。これらの項目はドロップダウンメニューにリストされ、iframeに表示されますが、各項目は完全なURLを使用して独自のページで開くことができます。スクラップしたURLを変更してその内線番号を変更する

これまでのところ、私のコードはオプションを見つけ、ページのベースアドレスに部分的なURLを付加して各オプションの完全なURLを作成し、.tifと.TIFのURLから最後の "/"を削除し、 .pdf "

しかし、.tifと.TIFのURLについては、新しいページで開くために "convert"を "pdf"に変更する必要があります。 .tif.pdfと.TIF.pdfのURLだけにこれを行う方法はありますか?

from urllib.request import urlopen as uReq 
from bs4 import BeautifulSoup as soup 
import os 

my_url = 'http://example.com' 
uClient = uReq(my_url) 
page_html = uClient.read() 
uClient.close() 

page_soup = soup(page_html, "html.parser") 

options = page_soup.findAll("select",{"id":"images"})[0].findAll("option") 
values = [o.get("value") for o in options] 

split_values = [i.split("|", 1)[0] for i in values] 
# The option value is split to separate the url from its label 
# <option value="/convert/ASRIMG/new/hop.TIF/|New Form"></option> 

new_val = [] 
for val in split_values: 
    ext = os.path.splitext(val.rstrip('/'))[-1] 
    new_ext = ext 
    if ext.lower() == '.tif': 
     new_ext += '.pdf' 
    new_val.append(val.rstrip('/').replace(ext, new_ext)) 

for i in range (len(new_val)): 
    image_urls = ('http://example.com' + new_val[i]) 

私の現在の結果:

print (new_val) 

/ASRIMG/good.jpg 
/ASRIMG/foo/bar1.jpg 
/ASRIMG/foo/bar2.jpg 
/ASRIMG/foo/bar3.jpg 
/convert/ASRIMG/new/hop.TIF.pdf 
/convert/REG/green1.tif.pdf 
/convert/REG//green2.tif.pdf 
/convert/SHIP/green3.tif.pdf 
/convert/SHIP/green4.tif.pdf 
/convert/SHIP/green5.tif.pdf 
/SKETCHIMG/001.png 
/SKETCH/002.JPG 


print (image_urls) 

http://example.com/ASRIMG/good.jpg 
http://example.com/ASRIMG/foo/bar1.jpg 
http://example.com/ASRIMG/foo/bar2.jpg 
http://example.com/ASRIMG/foo/bar3.jpg 
http://example.com/convert/ASRIMG/new/hop.TIF.pdf 
http://example.com/convert/REG/green1.tif.pdf 
http://example.com/convert/REG//green2.tif.pdf 
http://example.com/convert/SHIP/green3.tif.pdf 
http://example.com/convert/SHIP/green4.tif.pdf 
http://example.com/convert/SHIP/green5.tif.pdf 
http://example.com/SKETCHIMG/001.png 
http://example.com/SKETCH/002.JPG 

私は必要なもの:

http://example.com/ASRIMG/good.jpg 
http://example.com/ASRIMG/foo/bar1.jpg 
http://example.com/ASRIMG/foo/bar2.jpg 
http://example.com/ASRIMG/foo/bar3.jpg 
http://example.com/pdf/ASRIMG/new/hop.TIF.pdf 
http://example.com/pdf/REG/green1.tif.pdf 
http://example.com/pdf/REG//green2.tif.pdf 
http://example.com/pdf/SHIP/green3.tif.pdf 
http://example.com/pdf/SHIP/green4.tif.pdf 
http://example.com/pdf/SHIP/green5.tif.pdf 
http://example.com/SKETCHIMG/001.png 
http://example.com/SKETCH/002.JPG 

答えて

0

このステップの後:

split_values = [i.split("|", 1)[0] for i in values] 

このコードでは、上下のTIFの両方を処理します

In [48]: import os 

In [49]: split_values = ['/ASRIMG/good.jpg', '/convert/ASRIMG/new/hop.TIF/', 'SK 
    ...: ETCHIMG/001.png'] 

In [50]: new_val = [] 

In [51]: for val in split_values: 
    ...:  ext = os.path.splitext(val.rstrip('/'))[-1] 
    ...:  new_ext = ext 
    ...:  if ext.lower() == '.tif': 
    ...:   new_ext += '.pdf' 
    ...:  new_val.append(val.rstrip('/').replace(ext, new_ext)) 
    ...: 
    ...: 

これは右側からsplit_valuesリストからそれぞれの値から.tif/を取り除き、その後

+0

最後に.tif.pdfを追加し、あなたの迅速な返信いただきありがとうございます。私はこのコードを試しましたが、すべてのURLに '.tif.pdf 'を追加しました。 '.tif.pdf'(それは良い)に加えて、私は' .jpg.tif.pdf'、 '.TIF.tif.pdf'、' png.tif.pdf'を持っています)。 – shybr

+0

'.TIF /'を修正するコードを編集しました。 '.jpg.tif'と' .png.tif'の出力はどれくらいですか? –

+0

'.jpg'、' .JPG'、 '.png'は変更しないでください。 '.TIF /'と '.tif /'のみが '.TIF.pdf'と' .tif.pdf'に変更されます – shybr

関連する問題