2017-04-22 145 views
-1

所以下面的代碼工作正常,是爲網絡報廢一個報紙,在某個日期(即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() 
+0

什麼你的意思是「獲得空值」?哪裏?在輸出文件中?你的'print'語句怎麼樣? –

+0

將問題縮小到[MCVE]。不應該在每個問題上重複這個指示! –

回答

0

了我在for循環numpy.string_不是字符串格式,你可以在該行url改變%r%s,並改變strftime("%d/%m/%y")strftime("%d/%m/%Y")象下面這樣:

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%s-c30' % i 
+0

這完全是錯誤的! format'trftime(「%d /%m /%y」)'在我的URL中返回了我需要的錯誤字符串,這就是爲什麼我在錯誤的鏈接中搜索的原因,這就是BS對象爲空的原因。只要我改變了時間(「%d /%m /%Y」)',我就得到了預期的結果。謝謝@ Tiny.D!並感謝@BoundaryImposition它是非常有用的閱讀發佈問題和調試。 –