【问题标题】:Python writing in file [duplicate]Python写入文件[重复]
【发布时间】:2018-03-23 17:23:19
【问题描述】:

如何在我没有到达的file.txt 中写(i['src'])

这是我的代码

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'])

【问题讨论】:

  • 您能否在这一行中进一步澄清一下如何在我没有到达的file.txt 中写(i['src'])
  • 谢谢你的回答,我不想打印,但存储在一个文本文件( .write )对不起我的英语我使用谷歌翻译

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


【解决方案1】:

检查以下答案: 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

【讨论】:

    猜你喜欢
    • 2011-06-10
    • 1970-01-01
    • 2016-02-15
    • 2016-12-07
    • 2013-04-22
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多