使用硒的网页抓取

时间:2021-03-29 14:22:04

标签: python selenium web-scraping dropdown

我对 Python 非常陌生,我正在尝试从 Muthoot 网站获取所有商店位置。以下是我写的代码,但我没有得到任何输出。请让我知道什么是错的,我需要纠正什么。 据我了解,代码没有点击搜索按钮,因此没有任何移动。但是怎么做呢??

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
import pandas as pd
driver= webdriver.Chrome(executable_path="D:\Chromedriverpath\chromedriver_win32\chromedriver.exe")
driver.get("https://www.muthootfinance.com/branch-locator")
#Saving this element in a variable
drp=Select(driver.find_element_by_id("statelist"))
slist=drp.options

for ele in slist:    
    table=driver.select("table.table")
    columns=table.find("thead").find_all("th")
    column_names=[c.string for c in columns]
    table_rows=table.find("tbody").find_all("tr")
    l=[]
    for tr in table_rows:
        td=tr.find_all('td')
        row=[str(tr.get_text()).strip() for tr in td]
        l.append(row)
    
df=pd.DataFrame(l,columns=column_names)  
df.head()

1 个答案:

答案 0 :(得分:0)

我认为这现在对你有用,我复制了你的代码,它似乎有效!

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
import pandas as pd

driver = webdriver.Chrome("C:\Program Files (x86)\chromedriver.exe")
driver.get("https://www.muthootfinance.com/branch-locator")
# Saving this element in a variable

html_list = driver.find_element_by_id("state_branch_list")
items = html_list.find_elements_by_tag_name("li")
for item in items:
    places = item.text
    print(places)


df = pd.DataFrame([places])