我正在使用Selenium和Python抓取一个汽油/天然气价格网站,在首页上抓取最优惠的价格,我已经这样做了,然后搜索邮政编码/邮政编码"6076";并为该地区和周边郊区提供最优惠的价格。
这就是我被卡住的地方,因为它正在用HTML返回一个表。根据我下面的代码,我已经提取了一些表行;今天";价格和从表的第一行获取它的位置。
{hills}{local}和{price}是保存的变量,用于以后引用到自动SMS中。
如有任何帮助,我们将不胜感激。
from webdriver_manager.chrome import ChromeDriverManager
browser = webdriver.Chrome(ChromeDriverManager().install())
browser.execute_script("window.open('https://www.fuelwatch.wa.gov.au/fuelwatch/pages/home.jspx');")
browser.switch_to.window(browser.window_handles[-1])
elem = browser.find_element_by_css_selector('#homepage:j_idt84:bestMetroPrices:0:j_idt96:1:bestMetroPrice')
print("Best Petrol Price in Perth today is:", elem.text)
price = elem.text
elem = browser.find_element_by_css_selector('#searchform:product_label')
elem.click()
elem = browser.find_element_by_css_selector('#searchform:product_1')
elem.click()
elem = browser.find_element_by_css_selector('#searchform:location_input').send_keys("6076")
elem = browser.find_element_by_css_selector('#searchform:search')
elem.click()
browser.switch_to.window(browser.window_handles[-1])
elem = browser.find_elements_by_xpath("//table/tbody/tr[1]/td[1]")
for value in elem:
print("Best Petrol Price Kalamunda and surrounding areas today is:", value.text)
local = elem.text
elem = browser.find_element_by_css_selector('#searchResultForm:searchResultsTbl:0:tooltip-tradingName')
print("At:", elem.text)
hills = elem.text
import messagebird
client = messagebird.Client('XXXXXXXXXXXXXXXXXX')
message = client.message_create(
'+61XXXXXXXXX',
'+61XXXXXXXXX',
f'User, the Best Petrol Price in Perth today is: {price} at CostCo Perth Airport. The best price in Kalamunda and surrounding areas is {local} at {hills}. ', #Message goes here
{ 'reference' : 'Foobar' }
要从最后一页获得第一行的详细信息,可以执行以下操作:
CSS_SELECTOR
将是:
tr.ui-widget-content.ui-datatable-even[data-ri='0'] td
for details in driver.find_elements(By.CSS_SELECTOR, "tr.ui-widget-content.ui-datatable-even[data-ri='0'] td")
print(details.text)
如果要检索第二行,请更改任何行的[data-ri='1']
,依此类推。