【问题标题】:Python 3 Open URL line by line from file or listPython 3 从文件或列表中逐行打开 URL
【发布时间】: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

标签: python printing get


【解决方案1】:

所以我自己想办法解决这个问题

我把初始的id变量改成如下;

ids = [i.get_attribute('id').replace("name-", "https://www.someurlhere.com/page.html?id=") for i in 
browser.find_elements_by_xpath('.//*[@id[starts-with(.,"name")]]')]

所以我基本上用我想在打印上附加的 url 替换了 name- (行首)。这样我就可以做一个 browser.get(id) 并逐行打开每个 url。

附加代码如下:

for id in ids:
    print(id,sep='')
    browser.get(id) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    • 2017-05-21
    相关资源
    最近更新 更多