所以下面的代码工作正常,是为网络报废一个报纸,在某个日期(即01/04/2017),但是当我创建一个日期范围,并在每一天循环,我得到空值,下面的代码,然后我把'for loop'为循环返回空值
url = 'http://servicios.lanacion.com.ar/archivo-f01/04/2017-c30'
sauce = uReq(url)
soup = bs.BeautifulSoup(sauce,'html.parser')
date = soup.findAll('div',{'class':'titFecha'})
date = str(date)
fecha = '"titFecha">'
date = date[date.find(fecha)+len(fecha):date.find(fecha)+21]
day = date[:2]
month = date[3:5]
year = date[6:12]
print date
filename = 'La Nacion_%s_%s_%s.csv' % (day, month, year)
f = open(filename,'w')
headers = "Date, Title, Encabezado\n"
f.write(headers)
acumulados = soup.findAll('li',{'class':'acumulados'})
for acum in acumulados:
title = acum.a['href']
# title = title.text
print title
encabezado = acum.p
# encabezado = encabezado.text
print encabezado,'\n'
f.write(str(date) + ',' + str(title).replace(',',' ') + ',' + str(encabezado).replace(',',' ') + '\n')
f.close()
这里是循环,可能很容易,但我学习,还看不出这些问题,谢谢!
date_range = pd.date_range('2017-04-01',periods=2, freq='d')
date_range = date_range.strftime("%d/%m/%y")
for i in date_range:
url = 'http://servicios.lanacion.com.ar/archivo-f%r-c30' % i
sauce = uReq(url)
soup = bs.BeautifulSoup(sauce,'html.parser')
date = soup.findAll('div',{'class':'titFecha'})
date = str(date)
fecha = '"titFecha">'
date = date[date.find(fecha)+len(fecha):date.find(fecha)+21]
day = date[:2]
month = date[3:5]
year = date[6:12]
print date
filename = 'La Nacion_%s_%s_%s.csv' % (day, month, year)
f = open(filename,'w')
headers = "Date, Title, Encabezado\n"
f.write(headers)
acumulados = soup.findAll('li',{'class':'acumulados'})
for acum in acumulados:
title = acum.a['href']
print title
encabezado = acum.p
print encabezado,'\n'
f.write(str(date) + ',' + str(title).replace(',',' ') + ',' + str(encabezado).replace(',',' ') + '\n')
f.close()
什么你的意思是“获得空值”?哪里?在输出文件中?你的'print'语句怎么样? –
将问题缩小到[MCVE]。不应该在每个问题上重复这个指示! –