所以下面的代碼工作正常,是爲網絡報廢一個報紙,在某個日期(即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]。不應該在每個問題上重複這個指示! –