python3 简单的天气预报爬虫


import requests
import re
from prettytable import PrettyTable
 
url='http://www.tianqi.com/wuxi1/7/'
 
#加入头部信息,防止反爬
headers={
"user-agent":'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.6821.400 QQBrowser/10.3.3040.400',
'Host':'www.tianqi.com'
}
txt=requests.get(url,headers=headers).text
print(txt)

#用正则表达式解析每个信息
m1=re.compile(r'>(\d\d月\d\d日)<',re.S)#日期
m2=re.compile(r'class="week">(.+)')#星期
m3=re.compile(r'>(.{1,4})')#空气
m4=re.compile(r'class="temp">(.{1,5})')#天气
m5=re.compile((r'class="txt">(.+)℃ ~ (.+)℃'))#温度
m6=re.compile(r'class="txt">(\S+风.+)')#风速

m1_a = m1.findall(txt)
print(m1_a)
for i in m1_a:
    str_a = ''.join(i)
    #print(str_a)

m2_a = m2.findall(txt)
print(m2_a)
for i in m2_a:
    str_a = ''.join(i)
    #print(str_a)
    
m3_a = m3.findall(txt)
print(m3_a)
for i in m3_a:
    str_a = ''.join(i)
    #print(str_a)

m4_a = m4.findall(txt)
print(m4_a)
for i in m4_a:
    str_a = ''.join(i)
    #print(str_a)

m5_a = m5.findall(txt)
print(m5_a)
m5_a_1=[]
for i in m5_a:
    str_a = '~'.join(i)
    print(str_a)
    m5_a_1.append(str_a)
print('new list')
print(m5_a_1)    
    
m6_a = m6.findall(txt)
print(m6_a)
for i in m6_a:
    str_a = ''.join(i)
    #print(str_a)

#将爬取的数据保存到excel
#打开已有工作簿(支持绝对路径和相对路径)
import xlwings as xw
app = xw.App(visible=True,add_book=False)
#visible是否可见,add_book是否新建工作簿
wb = app.books.open(r'E:\Python\hby\PC\PC.xlsx')

#初始化vbasheet里面写入的值
wb.sheets['123'].range('a1:a1').value = m1_a
wb.sheets['123'].range('a2:a2').value = m2_a
wb.sheets['123'].range('a3:a3').value = m3_a
wb.sheets['123'].range('a4:a4').value = m4_a
wb.sheets['123'].range('a5:a5').value = m5_a_1
wb.sheets['123'].range('a6:a6').value = m6_a

# 保存数据
wb.save(r'E:\Python\hby\PC\PC.xlsx')

你可能感兴趣的:(学习笔记)