找到所需的元素后,我有了这个:
[<div class="statsValue">$1,615,422</div>, <div class="statsValue">1</div>, <div class="statsValue">2</div>]
我只想得到数字$1,615,422
。我该怎么做呢?我找不到任何有用的在线内容。
这是我的代码:
from selenium import webdriver
from selenium.webdriver.remote import webelement
import pandas as pd
import time
from bs4 import BeautifulSoup
driver = webdriver.Chrome('chromedriver.exe')
driver.get('https://www.redfin.com/')
search_box = driver.find_element_by_name('searchInputBox')
search_box.send_keys('693 Bluebird Canyon Drive, Laguna Beach, CA 92651')
search_box.submit()
time.sleep(2)
def get_address_url(address):
url_list = []
search_box = driver.find_element_by_name('searchInputBox')
search_box.send_keys('693 Bluebird Canyon Drive, Laguna Beach, CA 92651')
search_box.submit()
time.sleep(2)
url_list.append(driver.current_url)
# element = driver.find_elements_by_class_name('statsValue')
# print(element[0].get_attribute('innerHTML'))
soup = BeautifulSoup(driver.page_source, 'html.parser')
data = soup.find_all(lambda tag: tag.name == 'div' and tag.get('class') == ['statsValue'])
print(data)
print(len(data))
print(type(data))
driver.quit()
答案 0 :(得分:1)
您需要text
属性。
data = soup.find_all(lambda tag: tag.name == 'div' and tag.get('class') == ['statsValue'])
for element in data:
print (element.text)
答案 1 :(得分:1)
您可以使用正则表达式和sub
删除数字:
import re
price = re.sub("[^0-9]", "", "$1,615,422")
print(price)
结果:
1615422
soup = BeautifulSoup(driver.page_source, 'html.parser')
stats = soup.select(".statsValue")
for s in stats:
print(s.text)
答案 2 :(得分:1)
如果您只想获取数字$1,615,422
,我认为requests
就足够了。
希望此代码对您有帮助
import requests
from bs4 import BeautifulSoup as Soup
url = 'https://www.redfin.com/CA/Laguna-Beach/693-Bluebird-Canyon-Dr-92651/home/4894466'
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36'
}
r = requests.get(url, headers=headers)
soup = Soup(r.text, 'html5lib')
data = soup.find('div', {'class', 'avm'}).div.text
print(data) # $1,615,422