2017-11-14 6 views
1

私はhtml tdタグから単価値を取得しようとしていますが、同じクラス名の他のtdがあります。下記の画像を参照してください。ここenter image description herePythonのtdからのみ価格を選択

は私がループのために価格をスライスした場合、それが唯一の最初の価格を取得しますが、私は一度にすべての価格を持ちたい

from builtins import any as b_any 
from urllib.parse import urlparse 
from urllib.parse import urljoin 
from collections import Counter 
import urllib.request 
import csv 
import schedule 
import time 
import re 
from bs4 import BeautifulSoup 

url="http://offer.ebay.es/ws/eBayISAPI.dll?ViewBidsLogin&item=122713288532&rt=nc&_trksid=p2047675.l2564" 

req = urllib.request.Request(url, headers={'User-agent': 'Mozilla/5.0'}) 

htmlpage = urllib.request.urlopen(req) 

html = htmlpage.read().decode('utf-8') 

soup = BeautifulSoup(html,"html.parser") 

table = soup.find_all('td',{'class':'onheadNav'}) 

'''for txt in table: 
    nametxt = txt.text 
    result = ''.join([i for i in nametxt if not i.isdigit()]) 
    cleantxt = result.replace('(','') 
    print(cleantxt.replace(')','')) 

    rank = txt.a.text 
    print(rank)''' 
price = soup.select('td.contentValueFont') 
for pr in price: 
    print(pr.text) 

を書かれているコードがあります。

編集説明: 私はすべての価格をキャプチャしたいが、同じクラス名のtdが3つあり、Cantidad(数量)の価格1と、これらのすべてが同じクラスです。私が価格セクションだけを取得しようとすると、私のコードは3つのtdを返します。私はあなたが今、それを得る

+0

あなたの説明では矛盾しています:**単価の値を取得する** <-> **すべての値をすぐに持ちたい**。あなたの質問を更新してください – RomanPerekhrest

+0

私はすべての価格をキャプチャしたいのですが、問題は同じクラス名を持つ3つのtdがあります.dはCantidad(数量)の価格の1つで、日付のものはすべて同じクラスです。私が価格セクションだけを取得しようとすると、私のコードは3つのtdを返します。あなたは今それを得ることを願っています。 –

+0

私はbeautifulsoupをよく知っていませんが、クラスごとに取得するのではなく、td(例えば 'td [1]')を取得しようとするかもしれません。 – Stephan

答えて

1

怠惰な方法を望む:

soup = BeautifulSoup(html,"html.parser") 

table = soup.find_all('table') 

trs = table[9].select('tr') # You should select the table first (use your way) 

for tr in trs: # loop the tr in the table 
    if len(tr.select('td')) > 2: # check length 
     print(tr.select('td')[2].text) # select third td 
0

をショートソリューション:

from bs4 import BeautifulSoup 
import requests 

url = "http://offer.ebay.es/ws/eBayISAPI.dll?ViewBidsLogin&item=122713288532&rt=nc&_trksid=p2047675.l2564" 
html = requests.get(url).content 
soup = BeautifulSoup(html, "html.parser") 

prices =[ price.string.replace('\xa0', ' ') 
      for price in soup.select('td.contentValueFont') if price.string.endswith('EUR')] 
print(prices) 

出力:

['4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '8,90 EUR', '8,90 EUR', '8,90 EUR', '8,90 EUR', '8,90 EUR', '8,90 EUR', '8,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '14,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR'] 
1

あなたがする必要がある何がすべて "を見つけることですtr 'タグを取り除き、すべての文字を繰り返して、特定の' td 'からテキストを取得します。このような

何か:

table = soup.find_all('table') 
for tr in table[9].find_all('tr')[1:-1]: 
    price = tr.find_all('td')[2].text.strip() 
    print(price) 

いくつかの研究の後、私たちは私たちが望むの表は、したがってtable[9]、ページ上の10番目のテーブルであることを知ることができます。また、私たちは最初と最後の 'tr'を望んでいないので、find_all('tr')[1:-1]

これがあなたの問題を解決することを願っています。

関連する問題