python webscraping使用Javascript处理请求的窗体

2024-06-16 08:25:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从此页中刮取结果页:

http://data.philly.com/philly/property/

我使用254 W Ashdale St作为我的试用条目,当我在浏览器中这样做时,它会引导我找到我在HTML中寻找的结果(尽管是相同的URL)。你知道吗

Python请求成功地将我输入的地址放入结果页,但是我无法获取所有者信息,这正是我试图获取的。我一直在尝试硒和幻影,我所做的一切都是工作。你知道吗

我也对表单操作感到困惑,它似乎只是与表单所在页面的URL相同。你知道吗

我很感激所有的建议和帮助!你知道吗


Tags: com信息httpurl表单data地址html
1条回答
网友
1楼 · 发布于 2024-06-16 08:25:02

Selenium负责几乎所有的事情,只需找到元素,输入信息,找到按钮,单击它,然后找到所有者,单击它并获取所需的信息。你知道吗

import selenium
from selenium import webdriver

driver = webdriver.Chrome()
driver.get('http://data.philly.com/philly/property/')

#enter the street address
driver.find_element_by_name('LOC').send_keys('254 W Ashdale St')
#click on the submit button
driver.find_element_by_name('sendForm').click()

#find the owner
owner_tag = driver.find_elements_by_tag_name('td')[2]
owner = driver.find_elements_by_tag_name('td')[2].text
print(owner)

#click on the owner
owner_tag.find_element_by_tag_name('a').click()

#get the table with the relevant info    
rows = driver.find_element_by_tag_name('tbody').find_elements_by_tag_name('tr')

#get the row with the sale prices
sale_prices = list()
for row in rows:
    sale_prices.append(row.find_elements_by_tag_name('td')[4].text)

print('\n'.join(sale_prices))

输出:

FIRSTNAME LASTNAME
$123,600.00
$346,100.00
[..]
$789,500.00

相关问题 更多 >