Как тут перебрать списки, чтобы записать каждый элемент в файл ?python
Есть вот такой код, он парсит вакансии с сайтов и ищет нужные теги, которые потом вставляет в форму, но данные которые парсятся, но никак не получается не получается вставить данные, которые парсятся, в файл
import requests
from bs4 import BeautifulSoup
from datetime import datetime, date, time # импорт библиотек и модулей
from urllib.parse import urlparse
orignalurl = input("originalurl : ")
employer = input("employer : ")
defaultcity = input("defaultcity : ")
defaultcategory = input("defaultcategory : ")
введите сюда код
response = requests.get(orignalurl)
soup = BeautifulSoup(response.text, "lxml")
list1 = []
lj9 = []
pd9 = []
lj = soup.find_all("guid")
pd = soup.find_all("pubdate")
ds = soup.find_all("description")
ds9 = []
lj9.extend(lj)
pd9.extend(pd)
ds9 = str(ds).split("</description>")
for child in soup.recursiveChildGenerator():
if child.name:
list1.append(child.name)
q = list1.count("item")
i =1
nj9 = []
for n in lj :
response1 = requests.get(str(n).lstrip("<guid>").rstrip("</guid>"))
soup1 = BeautifulSoup(response1.text , "lxml")
nj = soup1.find("title")
nj9.extend(nj)
k = 0
# парсинг сайта
file = ("<?xml version=""1.0"" encoding=""utf-8""?>",
"<source>",
"<publisher>Big ABC Corporation</publisher>",
"<publisherurl>http://www.examplejobsite.com</publisherurl>",
"<lastBuildDate>Fri, 10 Dec 2008 22:49:39 GMT</lastBuildDate>",
"<job>",
"<title><![CDATA[Sales Executive]]></title>",
"<date><![CDATA[Fri, 10 Dec 2005 22:49:398 GMT]]></date>",
"<referencenumber><![CDATA[unique123131]]></referencenumber>",
"<url><![CDATA[http://www.examplejobsite.com/viewjob.cfm?jobid=unique123131]]></url>",
"<company><![CDATA[Big ABC Corporation]]></company>",
"<city><![CDATA[Phoenix]]></city>",
"<state><![CDATA[AZ]]></state>",
"<country><![CDATA[US]]></country>",
"<postalcode><![CDATA[85003]]></postalcode>",
"<description><![CDATA[oao]]></description>",
"<salary><![CDATA[$50K per year]]></salary>",
"<education><![CDATA[Bachelors]]></education>",
"<jobtype><![CDATA[fulltime]]></jobtype>",
"<category><![CDATA[Category1]]></category>",
"<experience><![CDATA[5+ years]]></experience>",
"</job>",
"[...]" ,
"</source>")
for i in range(q) :
k+=1
i+=1
i = str(i)
name = "originalurl" + i + ".xml"
f = str(open(name , "w"))
f1 = f + i
f1 = open(name , "w")
for m in file:
f1.write(m + "\n")
f1.close()
name = "originalurl" + i + ".xml"
def replace (sourceText, replaceText):
f1 = open(name , "r")
txt = f1.read()
f1 = open(name, "w")
f1.write(txt.replace(sourceText, replaceText))
f1.close()
response = requests.get(orignalurl) #функция для замены данных в файле
soup = BeautifulSoup(response.text, "lxml")
list1 = []
lj9 = []
pd9 = []
lj = soup.find_all("guid")
pd = soup.find_all("pubdate")
ds = soup.find_all("description")
ds9 = []
lj9.extend(lj)
pd9.extend(pd)
ds9 = str(ds).split("</description>")
for n in lj :
response1 = requests.get(str(n).lstrip("<guid>").rstrip("</guid>"))
soup1 = BeautifulSoup(response1.text , "lxml")
nj = soup1.find("title")
nj9.extend(nj)
lj1 = lj9[0]
lj9.clear
pd1 = pd9[0]
pd9.clear
replace("Big ABC Corporation" , employer)
replace("http://www.examplejobsite.com" , orignalurl)
replace("Phoenix" , defaultcity)
replace("$50K per year" , "United Kingdom")
replace("Category1" , defaultcategory)
replace("Fri, 10 Dec 2008 22:49:39 GMT" , str(datetime.now()))
replace("http://www.examplejobsite.com/viewjob.cfm?jobid=unique123131" , str(lj1).lstrip("<guid>").rstrip("</guid>"))
replace("unique123131" , str(lj1).lstrip("<guid>").rstrip("</guid>") )
replace("Fri, 10 Dec 2005 22:49:398" , str(pd1) ) # вот здесь никак не получается записать в файл
каждый элемент списка ( либо весь список, либо последний элемент списка записывается во все файлы)