Python写在文件中

时间:2018-03-23 17:23:19

标签: python python-3.x web-scraping beautifulsoup python-requests

如何在我未到达的(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'])

1 个答案:

答案 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