python能爬国外的网站吗_Python爬取国外天气预报网站的方法

本文介绍如何使用Python爬虫从AccuWeather网站抓取国际天气预报信息,涉及多线程、正则表达式和数据提取。示例代码展示了如何获取不同地区的天气网址并保存到文件。
摘要由CSDN通过智能技术生成

本文实例讲述了Python爬取国外天气预报网站的方法。分享给大家供大家参考。具体如下:

crawl_weather.py如下:

#encoding=utf-8

import httplib

import urllib2

import time

from threading import Thread

import threading

from Queue import Queue

from time import sleep

import re

import copy

lang = "fr"

count = 0

class Location:

# Location(False, "中国", "北京", "zh")

# Location(True, "", "亚洲", "zh")

def __init__(self, is_beyond_country, country_name, loc_name, lang):

self.country_name = country_name

self.loc_name = loc_name

self.lang = lang

self.is_beyond_country = is_beyond_country

prn_lock = threading.RLock()

def GetLocationURLs(url, recursive):

global count

if url.find("weather-forecast") != -1:

count = count + 1

if count % 500 == 0:

prn_lock.acquire()

print "count:%d" % (count)

prn_lock.release()

return [url]

page = urllib2.urlopen(url).read()

time.sleep(0.01)

#"

Africa
"

pattern = "

(.*)
"

locs = re.findall(pattern, page)

locs = [(url, name) for url, name in locs if url.find("browse-locations") != -1 or url.find("weather-forecast") != -1]

if not recursive:

urls = [url for url, name in locs]

return urls

urls =

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值