2017-01-21 10 views
1

私は車のデータを削っています。彼らの 'id'タグは1だけインクリメントされますが、どうやってそれを行うのか分かりません。美しいスープを使ったシンプルなPython Webスクレーパー

import bs4 as bs 
import urllib 

source = urllib.request.urlopen('http://www.25thstauto.com/inventory.aspx?cursort=asc&pagesize=500').read() 
soup = bs.BeautifulSoup(source, 'lxml') 

#finds the total number of cars 
count = soup.find('span', {'id': 'ctl00_cphBody_inv1_lblVehicleCount'}).getText()[:2] 
count = int(count) 

i = 1 
for url in range(1,count): 
url = soup.find_all('a', {'id': 'ctl00_cphBody_inv1_rptInventoryNew_ctl0'+i+'_nlVehicleDetailsTitle'}) 
    print(url['href']) 
    i = i + 1 
+0

私はなぜこの質問が下落されたのかわかりません、私はあなたのためにupvoted。 –

答えて

1
import bs4 as bs 
import urllib 
import re 

source = urllib.request.urlopen('http://www.25thstauto.com/inventory.aspx?cursort=asc&pagesize=500').read() 
soup = bs.BeautifulSoup(source, 'lxml') 

for a in soup.find_all('a', id=re.compile('ctl00_cphBody_inv1_rptInventoryNew')): 
    print(a.get('href')) 

アウト:

for a in soup.select('a[id*=ctl00_cphBody_inv1_rptInventoryNew]'): 
    print(a.get('href')) 

2008_Chevrolet_Malibu_Easton_PA_265928462.veh 
2008_Chevrolet_Malibu_Easton_PA_265928462.veh 
2008_Chevrolet_Malibu_Easton_PA_265928462.veh 
2002_Nissan_Xterra_Easton_PA_266894015.veh 
2002_Nissan_Xterra_Easton_PA_266894015.veh 
2002_Nissan_Xterra_Easton_PA_266894015.veh 
2009_Chevrolet_Cobalt_Easton_PA_265621796.veh 
2009_Chevrolet_Cobalt_Easton_PA_265621796.veh 

使用regexそのid属性ctl00_cphBody_inv1_rptInventoryNew

または使用CSS selector含まaタグを見つけるために、ここで私が持っているものです

アイデアは同じです。

+1

あなたはstackoverflowの王国の農民の中の神です –

+0

@サン・ケリーはdownvoteを無視し、私は誰かdownvoteすべてがあると思う。このような質問があれば、 'beautifulsoup'でタグ付けしてください。私はあなたのためにそこにいます。 –

+0

ありがとうございます。私はもっ​​と持っているかもしれないので、調整しておいてください! –

関連する問題