鱼C论坛

 找回密码
 立即注册
查看: 1045|回复: 6

爬虫代码疑问

[复制链接]
发表于 2019-3-27 17:15:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
今天一直为一个天气爬虫代码纠结。。
终于在各位的帮忙下基本完成了。但是有个奇怪的问题,我设置了函数,想直接输入一个年份,可以一次性爬取从2011年到指定年份的所有天气数据。爬到05月份的时候就报错了。。
但是,如果是年份是单独输入的话,则不会报错。。
详细代码如下(一次性爬取):
  1. import pandas as pd
  2. import numpy as np
  3. from bs4 import BeautifulSoup as bs
  4. import requests as res
  5. import re
  6. import time

  7. def wea(year):
  8.     for i in range(2011,year):
  9.         for j in range(1,13):
  10.             if len(str(j))<2:
  11.                 j = '0'+ str(j)
  12.             else:
  13.                 j = j
  14.             y = str(i)+str(j)
  15.             url = 'http://www.tianqihoubao.com/lishi/fujianfuzhou/month/%s.html'%y            
  16.             header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
  17.             res1 = res.get(url,headers = header)  #请求
  18.             soup = bs(res1.text,'lxml') #解析
  19.             cont1 = soup.find_all('td')
  20.             list1 = []
  21.             print('爬到第%s月了!'%y)
  22.             time.sleep(np.random.randint(1))
  23.             for i in cont1:
  24.                 weather = []
  25.                 for n in i.strings:
  26.                     a = n.replace(' ','').replace('\r','').replace('\n','')
  27.                     weather.append(a)
  28.                     total = (weather)
  29.                 list1.append(total)
  30.             list2 = []
  31.             for i in list1:
  32.                 for n in i:
  33.                     if n !='':
  34.                         list2.append(n)
  35.             list3 = []
  36.             for i in range(0,len(list2),4):
  37.                 date = list2[i]
  38.                 weather = list2[i+1]
  39.                 temp = list2[i+2]
  40.                 wind = list2[i+3]
  41.                 total =(date,weather,temp,wind)
  42.                 list3.append(total)
  43.             data = pd.DataFrame(list3)
  44.             data.to_csv(r"F:\数据资源\天气资料%s.csv"%year,encoding='gbk',header = None,mode='a')
  45. Wea(2019)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-3-27 17:16:09 | 显示全部楼层
只能爬取单年的代码如下:
  1. import pandas as pd
  2. import numpy as np
  3. from bs4 import BeautifulSoup as bs
  4. import requests as res
  5. import re
  6. import time

  7. def Wea(year):
  8.     for j in range(1,13):
  9.         if len(str(j))<2:
  10.             j = '0'+ str(j)
  11.         else:
  12.             j = j                    
  13.         url = 'http://www.tianqihoubao.com/lishi/fujianfuzhou/month/%s%s.html'%(year,j)
  14.         header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
  15.         res1 = res.get(url,headers = header)  #请求
  16.         soup = bs(res1.text,'lxml') #解析
  17.         cont1 = soup.find_all('td')
  18.         list1 = []
  19.         print('爬到第%s月了!'%j)
  20.         time.sleep(np.random.randint(1))
  21.         for i in cont1:
  22.             weather = []
  23.             for n in i.strings:
  24.                 a = n.replace(' ','').replace('\r','').replace('\n','')
  25.                 weather.append(a)
  26.                 total = (weather)
  27.             list1.append(total)
  28.         list2 = []
  29.         for i in list1:
  30.             for n in i:
  31.                 if n !='':
  32.                     list2.append(n)
  33.         list3 = []
  34.         for i in range(0,len(list2),4):
  35.             date = list2[i]
  36.             weather = list2[i+1]
  37.             temp = list2[i+2]
  38.             wind = list2[i+3]
  39.             total =(date,weather,temp,wind)
  40.             list3.append(total)
  41.         data = pd.DataFrame(list3)
  42.         data.to_csv(r"F:\数据资源\天气资料%s.csv"%year,encoding='gbk',header = None,mode='a')
  43. Wea(2015)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-27 17:38:00 | 显示全部楼层
是你的爬虫程序运行太快了,服务器响应不过来,你只需要设置一个睡眠时间就能解决
import time
然后在你的爬取一次程序末尾加上
time.sleep(2)
让这个程序睡2秒就可以了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-27 18:08:11 | 显示全部楼层
伏惜寒 发表于 2019-3-27 17:38
是你的爬虫程序运行太快了,服务器响应不过来,你只需要设置一个睡眠时间就能解决
import time
然后在你 ...

我用了sleep命令啊,你看第21/23行代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-27 18:11:47 | 显示全部楼层
伏惜寒 发表于 2019-3-27 17:38
是你的爬虫程序运行太快了,服务器响应不过来,你只需要设置一个睡眠时间就能解决
import time
然后在你 ...

而报错的是:
2.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-27 21:41:19 | 显示全部楼层

np.random.randint(1, size=10)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

返回值全是0,也就是time.sleep(0),哪里有让程序睡眠了?
你直接写time.sleep(2)就可以了,别搞那么麻烦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-28 08:22:20 | 显示全部楼层
伏惜寒 发表于 2019-3-27 21:41
np.random.randint(1, size=10)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

谢谢,我昨晚后面突然反应过来,发现for循环语句中,重复用了好几次i,是这个的问题。
你说的时间睡眠问题,我的确没做到位。只是不是根源问题。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-14 21:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表