Created
April 22, 2016 12:23
-
-
Save wangx2/5ab1b26a948a27bb741a887e00a30e57 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python | |
# -*- coding:utf-8 -*- | |
#代码有一些问题,不能输入中文,这个初期版,以后再修改 | |
import urllib.request | |
import re | |
def get_page(): | |
city = input('请输入你所在的城市:') | |
url = 'http://php.weather.sina.com.cn/search.php?f=1&c=1&city=' + city + '&dpc=1' | |
user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36' | |
headers = {'User-Agent':user_agent} | |
request = urllib.request.Request(url,None,headers) | |
response = urllib.request.urlopen(request) | |
get_html = response.read().decode('gbk') | |
re_title = r'var CITY = (.*);'#获取发布城市正则 | |
re_date = r'<span style="font-size:12px;">\((.*)\)</span>'#获取发布时间正则 | |
re_content = r'<span class="fs_30 tpte">([\S]{3})</span>'#获取发布内容正则 | |
re_shenme = r'<li>(\w*)</li>' | |
title = re.search(re_title,get_html) | |
date = re.search(re_date,get_html) | |
content = re.search(re_content,get_html) | |
shenme = re.search(re_shenme, get_html) | |
tianqi = shenme.group(0) | |
tianqi = tianqi[4:6] | |
if title: | |
print('你所在的城市:%s'%title.group(1)[1:3]) | |
else: | |
print('请确认你的城市:%s是否输入正确'%city) | |
if date: | |
print(date.group(1)) | |
if content: | |
print('今天的天气是:%s %s'%(content.group(1),tianqi)) | |
get_page() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment