2017-04-22 143 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它是非常有用的阅读发布问题和调试。 –