我正在用两个不同的脚本解析两个不同的 html 源(一个吐出“数据 A、B、C、D 和 E”,另一个吐出“数据 F”)。我想将这两个脚本的输出组合成一个简单的 csv 格式。
我正在尝试运行第三个脚本,该脚本从其他两个脚本中导入所有内容并打印出数据。这就是我正在做的尝试并实现这一目标:
#!usr/bin/env python
from script1 import *
from script2 import *
for c in cities :
c.retrieveTemps()
print(c.name,c.high0,c.low0,c.high1,c.low1,c.weather0,c.weather1,c.wind0,c.wind1)
所有变量都在 script1 和 script2 中定义。Script1 查找除 c.wind1 之外的所有变量。但是,当我运行上面的代码时,它只会找到 script1 或 script2 的数据(取决于我第二次导入的数据),而不是两者。
关于我可以做些什么来让它打印出 script1 和 script2 中的所有数据的任何想法?谢谢!
编辑
这是来自脚本 1:
#!usr/bin/env python
import re
import urllib
from datetime import datetime
from datetime import timedelta
date = datetime.now()
date1 = date + timedelta(days=1)
date2 = date + timedelta(days=2)
class city :
def __init__(self, city_name, link) :
self.name = city_name
self.url = link
self.wind1 = 0
def retrieveTemps(self) :
filehandle = urllib.urlopen(self.url)
# get lines from result into array
lines = filehandle.readlines()
# (for each) loop through each line in lines
line_number = 0 # a counter for line number
for line in lines:
line_number = line_number + 1 # increment counter
# find string, position otherwise position is -1
position2 = line.rfind('<ul class="stats">')
#String is found in line
if position2 > 0 :
self.wind0 = lines[line_number + 1].split('</strong>')[0].split('style="">')[-1]
break # done with loop, break out of it
return ('c.wind0')
filehandle.close()
m1 = city('Mexico City', 'http://www.accuweather.com/en/mx/mexico-city/242560/daily-weather-forecast/242560?day=2')
m3 = city('Veracruz', 'http://www.accuweather.com/en/mx/veracruz/236233/daily-weather-forecast/236233?day=2')
m5 = city('Tampico', 'http://www.accuweather.com/en/mx/tampico/235985/daily-weather-forecast/235985?day=2')
m7 = city('Nuevo Laredo', 'http://www.accuweather.com/en/mx/nuevo-laredo/235983/daily-weather-forecast/235983?day=2')
m9 = city('Monterrey', 'http://www.accuweather.com/en/mx/monterrey/244681/daily-weather-forecast/244681?day=2')
m11 = city('S. Luis Potosi', 'http://www.accuweather.com/en/mx/san-luis-potosi/245369/daily-weather-forecast/245369?day=2')
m13 = city('Queretaro', 'http://www.accuweather.com/en/mx/queretaro/245027/daily-weather-forecast/245027?day=2')
m15 = city('Laz. Cardenas', 'http://www.accuweather.com/en/mx/lazaro-cardenas/239054/daily-weather-forecast/239054?day=2')
cities = []
cities.append(m1)
cities.append(m3)
cities.append(m5)
cities.append(m7)
cities.append(m9)
cities.append(m11)
cities.append(m13))
cities.append(m15)