2017-02-10 82 views
1

我想提取從網站用戶BU使用循環,但我不知道我怎樣才能把正確的「我」,而不是數字1如何我可以我通過迭代數據在網站上

後我把

user_id = browser.find_element_by_xpath("(//div[@class='_gzjax'])["+str(i)+"]").text 

回溯(最近最後調用):文件 「d:/代碼/ Python的/ Instagram的 取消追蹤/ Instagram的Unfollow.py」,第32行,在 USER_ID = browser.find_element_by_xpath(「(/ /div[@class='_gzjax'])["+str (i)+"]").text 文件 「C:\ Python33 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py「, line 293,in find_element_by_xpath return self.find_element(by = By.XPATH,value = xpath)文件」C:\ Python33 \ lib \ site 'package'\ selenium \ webdriver \ remote \ webdriver.py「, line 752,in find_element 'value':value})['value']文件」C:\ Python33 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py「, 第236行,執行 self.error_handler.check_response(response)文件」C:\ Python33 \ lib \ site-packages \ selenium \ webdriver \ remote \ errorhandler.py「, line 192, in check_response raise exception_class(message,screen,stacktrace)selenium.common.exceptions.NoSuchElementException:消息:無法對 定位元素: {「method」:「xpa (文件:/// c:/ users/viktor)「,」選擇器「:」(// div [@ class ='gzjax'])[0]「} Stacktrace: at FirefoxDriver.prototype.findElementInternal /appdata/local/temp/tmp1r0vgw/extensions/[email protected]/components/driver-component.js:10770) at FirefoxDriver.prototype.findElement(file:/// c:/ users/viktor/appdata/local /temp/tmp1r0vgw/extensions/[email protected]/components/driver-component.js:10779) at DelayedCommand.prototype.executeInternal_/h(file:/// c:/ users/viktor/appdata/local/temp /tmp1r0vgw/extensions/[email protected]/components/command-processor.js:12661) at DelayedCommand.prototype.executeInternal_(file:/// c:/ users/viktor/appdata/local/temp/tmp1r0vgw/extensions /[email protected]/components/command-processor.js:12666) 在DelayedCommand.prototype.execute/<(文件:/// C:/users/viktor/appdata/local/temp/tmp1r0vgw/extensions/[email protected]/components/command-processor.js:12608)

+0

你不能連接xpath字符串中的i var例如「(// div [@class ='_ gzjax'])[」+ i +「]」 – user993553

+0

@ user993553我試過這樣的「+ i +」,但它給我錯誤,我想我;米丟失東西 – Viktor

+0

仔細檢查你的代碼。看起來像下劃線越來越被丟棄:這是在你的錯誤輸出:{「method」:「xpath」,「selector」:「(// div [@ class ='gzjax'])[0]」}所以concat正在工作 –

回答

1

正好連接i(第一它轉換爲字符串)到您想要的字符串:

for i in range (0,100): 
    user_id = browser.find_element_by_xpath("(//div[@class='_gzjax'])["+str(i)+"]").text 
    print(user_id) 
+0

給出了一個錯誤我更新的問題 – Viktor

+0

@Viktor這是不相關的把我作爲字符串在Xpath,這意味着你要找的元素不在頁面中。 – Arman

+0

哦,謝謝我弄清楚,我需要從1開始循環而不是從0開始。謝謝你 – Viktor

0

您將需要顯式轉換i從int到字符串。如何:

for i in range (0,100): 
    user_id = browser.find_element_by_xpath("(//div[@class='_gzjax'])["+str(i)+"]").text 
    print(user_id) 
1

試試這個。我想您連接變量納入字符串加在python登錄:

user_id = browser.find_element_by_xpath("(//div[@class='_gzjax'])["+i+"]").text 

看我們怎麼加(「字符串」 +變量+「串」)

+0

你不能用加號連接字符串和整數 –

0

我不知道有多少用戶有上該頁面,但因爲它站立你101.

只能循環通過元素?

for i in browser.find_element_by_xpath("(//div[@class='_gzjax'])"): 
    user_id = i.text 
    print(user_id) 

沿着這些行迭代項而不是索引的東西。