因此,下面的代码可以很好地工作,用于在特定日期(即01/04/2017)对报纸进行web报废,但是当我创建一个日期范围,并在每天循环时,我会在代码下面得到空值,然后我将“for循环”放入
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()
for循环中i的格式是numpy.string\不是字符串,您可以在
url
行中将%r
更改为%s
,并将strftime("%d/%m/%y")
更改为strftime("%d/%m/%Y")
,如下所示:相关问题 更多 >
编程相关推荐