【发布时间】:2017-07-19 14:53:55
【问题描述】:
需要帮助来弄清楚如何为多个 URL 做 browser.get
这是当前代码
#OPEN FILE
import sys
f = open("ids.txt", 'w')
sys.stdout = f
#GRAB IDS
ids = [i.get_attribute('id').replace("name-", "") for i in
browser.find_elements_by_xpath('.//*[@id[starts-with(.,"name")]]')]
#STATIC URL
url = ("https://www.someurlhere.com/page.html?id=")
#PRINT IDS & MAP TO STATIC URL
for id in ids:
print("https://www.someurlhere.com/page.html?id=",id,sep='')
#browser.get(url, id)
#This gives error get() takes 2 positional arguments but 3 were given
打印会生成一个文件 ids.txt,它会逐行显示所有 url 和 id 示例:
https://www.someurlhere.com/page.html?id=12345678
https://www.someurlhere.com/page.html?id=87654321
https://www.someurlhere.com/page.html?id=87283798
这是我要加载的正确网址
现在,我需要帮助来为列表 1 中的每个 url 按 1 执行 browser.get,然后从加载的每个页面中获取数据。
通常我会执行以下操作,但它不会产生我正在寻找的正确网址;
links = [i.get_attribute('href') for i in
browser.find_elements_by_xpath('.//a[contains(., "name")]')]
for url in links:
print (url, end=',')
browser.get(url)
time.sleep(2)
#then do something on each page
这可以工作并逐页打开,但会产生错误的网址
我需要一种方法来逐个打开每个网址
任何帮助表示赞赏
谢谢
【问题讨论】:
-
您复制并粘贴了这个吗?导致您的缩进(例如在
for循环中)关闭。 -
不,要打印的所有内容都可以正常工作。就在我尝试 browser.get 时,我无法让它按预期工作。在结果中依次加载 1 个 url