如何在我未到达的(i['src'])
中写file.txt
?
这是我的代码
from bs4 import BeautifulSoup
import requests
import re
a = requests.get('domaine.com')
soup = BeautifulSoup(a.content, 'lxml')
links = soup.find_all('iframe')
for i in links:
print(i['src'])
答案 0 :(得分:0)
检查以下答案: Correct way to write line to file?
代码:
from bs4 import BeautifulSoup
import requests
import re
a = requests.get('http://domaine.com')
soup = BeautifulSoup(a.content, 'lxml')
links = soup.find_all('iframe')
f = open('file.txt', 'a')
for i in links:
f.write(str(i['src']) + '\n')
file.txt的输出:
https://www.googletagmanager.com/ns.html?id=GTM-M6RJ9CB