पूरी डिव के बजाय सिर्फ ब्यूटीफुल नंबर से नंबर हासिल करना

Nov 24 2020

मैं जीवन को आसान बनाने के लिए एक छोटा सा websraping कार्यक्रम बनाकर अजगर को सीखने की कोशिश कर रहा हूं, हालांकि मुझे बीएस 4 का उपयोग करते समय केवल नंबर प्राप्त करने के साथ समस्या हो रही है। जब मैं एक वास्तविक विज्ञापन स्क्रैप करता हूं, तो मुझे कीमत मिल सकती थी, लेकिन मैं पृष्ठ से सभी मूल्य प्राप्त करना चाहूंगा।

यहाँ मेरा कोड है:

from bs4 import BeautifulSoup
import requests
prices = []
url = 'https://www.kijiji.ca/b-cars-trucks/calgary/new__used/c174l1700199a49?ll=51.044733%2C-114.071883&address=Calgary%2C+AB&radius=50.0'
result = requests.get(url)
print (result.status_code)
src = result.content
soup = BeautifulSoup(src, 'html.parser')
print ("CLEARING")
price = soup.findAll("div", class_="price")
prices.append(price)
print (prices)

यहाँ मेरा आउटपुट है

[<div class="price">
                        
                            
                            
                                
                                
                                    $46,999.00 <div class="dealer-logo"> <div class="dealer-logo-image"> <img src="https://i.ebayimg.com/00/s/NjBYMTIw/z/xMQAAOSwi9ZfoW7r/$_69.PNG"/>
</div>
</div>
</div>

आदर्श रूप में, मैं केवल यही चाहता हूं कि आउटपुट "46,999.00" हो।

मैंने टेक्स्ट = ट्रू के साथ कोशिश की, हालांकि यह काम नहीं किया और मुझे खाली सूची के अलावा इससे कोई आउटपुट नहीं मिलेगा।

धन्यवाद

जवाब

1 MendelG Nov 24 2020 at 22:54

RegEx का उपयोग किए बिना एक विकल्प, उन टैगों को फ़िल्टर करना है जो startwith()एक डॉलर चिह्न है $:

import requests
from bs4 import BeautifulSoup

URL = 'https://www.kijiji.ca/b-cars-trucks/calgary/new__used/c174l1700199a49?ll=51.044733%2C-114.071883&address=Calgary%2C+AB&radius=50.0'

soup = BeautifulSoup(requests.get(URL).content, "html.parser")

price_tags = soup.find_all("div", class_="price")

prices = [
    tag.get_text(strip=True)[1:] for tag in price_tags
    if tag.get_text(strip=True).startswith('$')
]

print(prices)

आउटपुट:

['48,888.00', '21,999.00', '44,488.00', '5,500.00', '33,000.00', '14,900.00', '1,750.00', '35,600.00', '1,800.00', '25,888.00', '36,888.00', '32,888.00', '30,888.00', '18,888.00', '21,888.00', '29,888.00', '22,888.00', '30,888.00', '17,888.00', '17,888.00', '16,888.00', '22,888.00', '22,888.00', '34,888.00', '31,888.00', '32,888.00', '30,888.00', '21,888.00', '15,888.00', '21,888.00', '28,888.00', '19,888.00', '18,888.00', '30,995.00', '30,995.00', '30,995.00', '19,888.00', '47,995.00', '21,888.00', '46,995.00', '32,888.00', '29,888.00', '26,888.00', '21,888.00']
3 ATIFADIB Nov 24 2020 at 13:14

आपको टैग का पाठ भाग प्राप्त करने की आवश्यकता है और फिर उस पर कुछ रेगेक्स प्रसंस्करण करें।

import re

def get_price_from_div(div_item):
    str_price = re.sub('[^0-9\.]','', div_item.text)
    float_price = float(str_price)
    return float_price

डिवेस खोजने के बाद अपने कोड में इस विधि को कॉल करें

from bs4 import BeautifulSoup
import requests
prices = []
url = 'https://www.kijiji.ca/b-cars-trucks/calgary/new__used/c174l1700199a49?ll=51.044733%2C-114.071883&address=Calgary%2C+AB&radius=50.0'
result = requests.get(url)
print (result.status_code)
src = result.content
soup = BeautifulSoup(src, 'html.parser')
print ("CLEARING")
price = soup.findAll("div", class_="price")
prices.extend([get_price_from_div(curr_div) for curr_div in price])
print (prices)