从文本文件中提取 URL
Extracting URLs from a text file
如何将我保存在文本文件中的 link 一个一个地提取出来,以便在我的其他代码中使用它:-
文本文件中 link 的示例是:-
http://www.barneys.com/givenchy-poplin-field-shirt-504068255.html
我的文本文件中只有 URL,一行中没有多个 URL。
我必须使用 link(s) 的其他代码是:-
import requests, re
from bs4 import BeautifulSoup
url=" " #here I have to use the links one by one
r=requests.get(url)
with open(file_name) as f:
urls = f.readlines()
urls = ([s.strip('\n') for s in urls ])
for url in urls:
# insert code here to do whatever you want with one url
这就是你想要的吗?这将读取每一行并将其存储到此列表中。
已编辑以去除列表元素中的“\n”字符
如何将我保存在文本文件中的 link 一个一个地提取出来,以便在我的其他代码中使用它:-
文本文件中 link 的示例是:-
http://www.barneys.com/givenchy-poplin-field-shirt-504068255.html
我的文本文件中只有 URL,一行中没有多个 URL。
我必须使用 link(s) 的其他代码是:-
import requests, re
from bs4 import BeautifulSoup
url=" " #here I have to use the links one by one
r=requests.get(url)
with open(file_name) as f:
urls = f.readlines()
urls = ([s.strip('\n') for s in urls ])
for url in urls:
# insert code here to do whatever you want with one url
这就是你想要的吗?这将读取每一行并将其存储到此列表中。
已编辑以去除列表元素中的“\n”字符