python3从文件或lis逐行打开URL

2024-10-01 19:14:41 发布

您现在位置:Python中文网/ 问答频道 /正文

需要帮忙弄清楚怎么办浏览器.get对于多个URL

这是当前的代码

#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文件它用id逐行显示所有url 示例:

https://www.someurlhere.com/page.html?id=12345678

https://www.someurlhere.com/page.html?id=87654321

https://www.someurlhere.com/page.html?id=87283798

这是我想要载入的正确网址

现在,我需要帮助浏览器.get对于列表1x1中的每个url,然后从加载的每个页面获取数据。在

通常我会做以下事情,但它不能产生正确的网址,我正在寻找

^{pr2}$

这可以工作并逐页打开,但会生成错误的url

我需要一种方法打开每个网址1×1

有什么帮助吗

谢谢


Tags: httpstxtcomididsurlgethtml
1条回答
网友
1楼 · 发布于 2024-10-01 19:14:41

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

我修改了初始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-(行首)。这样我就可以浏览器.get(id)并逐行打开每个url。在

附加代码如下:

^{pr2}$

相关问题 更多 >

    热门问题