Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中遍历多个url请求的值列表_Python_Pandas_Url_Web Scraping_Beautifulsoup - Fatal编程技术网

在python中遍历多个url请求的值列表

在python中遍历多个url请求的值列表,python,pandas,url,web-scraping,beautifulsoup,Python,Pandas,Url,Web Scraping,Beautifulsoup,我正试图从多个气象站获取多年的地下天气小时数据,并将其放入熊猫数据框中。我不能使用API,因为请求有限制,我不想花费数千美元来刮取这些数据 我可以得到脚本,从一个工作站上获取我想要的所有数据。当我试图修改它,使它在一个站点列表中循环时,我要么得到一个406错误,要么它只返回列表中第一个站点的数据。我怎样才能在所有车站转圈?另外,如何存储站点名称,以便将其添加到另一列的数据帧中 下面是我的代码现在的样子: stations = ['EGMC','KSAT','CAHR'] weather_d

我正试图从多个气象站获取多年的地下天气小时数据,并将其放入熊猫数据框中。我不能使用API,因为请求有限制,我不想花费数千美元来刮取这些数据

我可以得到脚本,从一个工作站上获取我想要的所有数据。当我试图修改它,使它在一个站点列表中循环时,我要么得到一个406错误,要么它只返回列表中第一个站点的数据。我怎样才能在所有车站转圈?另外,如何存储站点名称,以便将其添加到另一列的数据帧中

下面是我的代码现在的样子:

 stations = ['EGMC','KSAT','CAHR']


weather_data = []
date = []
for s in stations:
    for y in range(2014,2015):
        for m in range(1, 13):
            for d in range(1, 32):
            #check if a leap year
                if y%400 == 0:
                    leap = True
                elif y%100 == 0:
                    leap = False
                elif y%4 == 0:
                    leap = True
                else:
                    leap = False

            #check to see if dates have already been scraped    

            if (m==2 and leap and d>29):
                continue
            elif (y==2013 and m==2 and d > 28):
                continue
            elif(m in [4, 6, 9, 11] and d > 30):
                continue

            timestamp = str(y) + str(m) + str(d)
            print ('Getting data for ' + timestamp)

#pull URL
            url = 'http://www.wunderground.com/history/airport/{0}/' + str(y) + '/' + str(m) + '/' + str(d) + '/DailyHistory.html?HideSpecis=1'.format(stations)
            page = urlopen(url)

        #find the correct piece of data on the page
            soup = BeautifulSoup(page, 'lxml')



            for row in soup.select("table tr.no-metars"):
                date.append(str(y) + '/' + str(m) + '/' + str(d))
                cells = [cell.text.strip().encode('ascii', 'ignore').decode('ascii') for cell in row.find_all('td')]
                weather_data.append(cells)

weather_datadf = pd.DataFrame(weather_data)
datedf = pd.DataFrame(date)
result = pd.concat([datedf, weather_datadf], axis=1)
result

这是你的错误的解释

您应该将
用户代理
添加到标题中。但我认为在这个网站上存在一些防止爬行的保护措施,你应该使用更具体的东西,比如Scrapy、Crawlera、代理列表、用户代理旋转器