[英]Appending list from a for loop
我正在嘗試使用從網頁收集的鏈接填充列表。 該列表出乎意料的結果。 我使用的代碼有什么問題?
def get_contact_id(self):
wd = self.app.wd
elements = wd.find_elements_by_xpath("//a[contains(@href, 'edit.php?id')]")
print(elements)
links = []
for i in elements:
link = i.get_attribute('href')
links.append(link)
print(links)
我希望得到:
links = ['//192.168.1.22:8080/addressbook/edit.php?id=9','//192.168.1.22:8080/addressbook/edit.php?id=11','//192.168.1.22:8080/addressbook/edit.php?id=13','//192.168.1.22:8080/addressbook/edit.php?id=10','//192.168.1.22:8080/addressbook/edit.php?id=14']
相反,我得到:
links = ['//192.168.1.22:8080/addressbook/edit.php?id=9']
['//192.168.1.22:8080/addressbook/edit.php?id=9','//192.168.1.22:8080/addressbook/edit.php?id=10']
['//192.168.1.22:8080/addressbook/edit.php?id=9', '//192.168.1.22:8080/addressbook/edit.php?id=10', '//192.168.1.22:8080/addressbook/edit.php?id=11']
['//192.168.1.22:8080/addressbook/edit.php?id=9', '//192.168.1.22:8080/addressbook/edit.php?id=10', '//192.168.1.22:8080/addressbook/edit.php?id=11', '//192.168.1.22:8080/addressbook/edit.php?id=13']
['//192.168.1.22:8080/addressbook/edit.php?id=9', '//192.168.1.22:8080/addressbook/edit.php?id=10', '//192.168.1.22:8080/addressbook/edit.php?id=11', '//192.168.1.22:8080/addressbook/edit.php?id=13', '//192.168.1.22:8080/addressbook/edit.php?id=14']
您需要在for循環之外打印links
。
def get_contact_id(self):
wd = self.app.wd
elements = wd.find_elements_by_xpath("//a[contains(@href, 'edit.php?id')]")
print(elements)
links = []
for i in elements:
link = i.get_attribute('href')
links.append(link)
print(links)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.