有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用RSelenium在R中提交java表单

我正在尝试使用RSelenium在这里发布一个表单http://www.censusindia.gov.in/Census_Data_2001/Village_Directory/View_data/Village_Profile.aspx

我可以从下拉菜单中收集所有的值,但我不知道如何提交表单来检索表数据

我试过很多命令

#Setting up the proxy server
RSelenium::checkForServer()

#Openning the Remote Driver
remDr <- remoteDriver$new()
remDr$open()
remDr$setImplicitWaitTimeout(3000)
remDr$navigate("http://www.censusindia.gov.in/Census_Data_2001/Village_Directory/View_data/Village_Profile.aspx")

stateElem <- remDr$findElement(using = "name", "ctl00$Body_Content$drpState")
stateElem$setElementAttribute(value = "18") #for state "Assam"

remDr$executeScript("document.forms[0].submit();", list(value = "18"))

以及

document.forms["aspnetForm"].elements["stateElem"].value = "18"

我希望能够在选定州的地区、街道和村庄中运行这个循环

我知道我在这里没有给社区提供太多的合作机会,但我对RSelenium和java还不熟悉

提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    我和一位朋友(痛苦地)用Python找到了这个问题的解决方案。我仍然很想学习一种用R完成类似任务的方法。下面是代码:

    import mechanize 
    from bs4 import BeautifulSoup
    
    
    URL = "http://www.censusindia.gov.in/Census_Data_2001/Village_Directory/View_data/Village_Profile.aspx"
    
    def select_element(br, form, value):
        br.select_form(nr=0)
        br[form] = [value]
        return br.submit()
    
    def get_page(state, district, sub_district, village): 
        """ Get village data """
        # I have no idea why exactly this works, the form uses javascript callbacks and
        # it seems that you need to submit the form for each selection or you get an error.
        br = mechanize.Browser()
        br.open(URL)
        # Could probably parse the responses at each stage to get valid entries for the next sub-unit.
        select_element(br, 'ctl00$Body_Content$drpState', state)
        # read html and pull out disticts
        select_element(br, 'ctl00$Body_Content$drpDistrict', district)
        select_element(br, 'ctl00$Body_Content$drpSubDistrict', sub_district)
        r2 = select_element(br, 'ctl00$Body_Content$drpVillage', village)
        return r2.read()
    
    state = '35'
    district = '01'
    sub_district = '0004'
    village = '00026500'
    print(get_page(state, district, sub_district, village))
    
    parameters = {'35': 
                {'01': 
                {'0004': ['00026500']}}}
    
    foo = []        
    for state, districts in parameters.items():
        for district, subdistricts in districts.items():
            for subdistrict, villages in subdistricts.items():
                for village in villages:
                    foo.append(get_page(state, district, subdistrict, village))
                    with open('foo', 'w') as f:
                        f.write(get_page(state, district, subdistrict, village))
    

    我对Python非常陌生,更喜欢使用R来完成这项任务。这可能吗