国外印花图案设计网站,苏州市规划建设局网站,遂宁北京网站建设,网站建设资源kindlePython不仅用于网站开发#xff0c;数据分析#xff0c;图像处理#xff0c;也常用于爬虫技术方向#xff0c;最近学习了解下#xff0c;爬虫技术入门一般先使用bs4库#xff0c;爬取天气预报简单尝试下。
第一步#xff1a;首先选定目标网站地址
网上查询#xff0c…Python不仅用于网站开发数据分析图像处理也常用于爬虫技术方向最近学习了解下爬虫技术入门一般先使用bs4库爬取天气预报简单尝试下。
第一步首先选定目标网站地址
网上查询天气预报准确率高的官网是“天气网”网址http://www.weather.com.cn/
第二步确定爬取目标数据
本次只是简单学习尝试下。
1爬取-今天-天气预报天气情况最高温度最低温度
2爬取-近7天-天气预报日期天气温度风力 第三步确定爬取目标数据网页元素定位
例如今天-最高温度页面元素定位使用浏览器开发调试功能按 F12 进入。 调试界面左上角箭头点击进入选择元素模式然后从页面中选择需要查看的元素然后可以在开发者工具元素Elements一栏中定位到该元素源代码的具体位置 。 查看元素属性可从被定位的源码中查看部分如class、src也可在右边的侧栏中查看全部的属性如下图位置查看 根据页面元素层级关系确定bs4库的BeautifulSoup选择元素位置根据元素标签属性层级递减最后确定目标元素位置即可。
# 最高气温-元素位置如下
soup.select(div.t ul.clearfix li p.tem)
以此类推确定所有需要爬取的目标元素位置。 第四步完成代码编写
requests_bs4.py :
# -*- coding: UTF-8 -*-
# 爬取静态网页工具
import requests
import time
import re
from bs4 import BeautifulSoup
import randomdef get_html_text(url):方法名称: 获取网页的html信息中文注释: 获取网页的html信息,转换成字符串格式数据入参:param url str 网址出参:返回状态:return 0 失败或异常return 1 成功返回错误码返回错误信息param rsp_text str 网页html信息作 者: PandaCode辉创建时间: 2023-09-05使用范例: get_html_text(https://www.baidu.com/)try:if (not type(url) is str):return [0, 111111, 网址参数类型错误,不为字符串, [None]]# 浏览器用户信息列表user_agents [Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11,Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0,Opera/9.25 (Windows NT 5.1; U; en),Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727),Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu),Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12,Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9,Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.7 (KHTML, like Gecko) Ubuntu/11.04 Chromium/16.0.912.77 Chrome/16.0.912.77 Safari/535.7,Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:10.0) Gecko/20100101 Firefox/10.0,]# 随机获取一个浏览器用户信息agent random.choice(user_agents)# header头信息headers {User-Agent: agent,Accept: text/html,application/xhtmlxml,application/xml;q0.9,*/*;q0.8,Accept-Language: zh-CN,zh;q0.8,en-US;q0.5,en;q0.3,Accept-Encoding: gzip, deflate,Connection: keep-alive,Cache-Control: max-age0,}# 代理IP地址需要找到可用的代理ip不然无法使用# proxy {HTTP: xx.xx.xx.xx:8000, HTTPS: xx.xx.xx.xx:80}# response requests.get(url, headersheaders, proxiesproxy, timeout30)# 增加随机模拟浏览器访问header头信息提高反爬网站成功率response requests.get(url, headersheaders, timeout30)# print(response.status_code)response.raise_for_status()response.encoding utf-8rsp_text response.text# 返回容器return [1, 000000, 获取网页的html信息成功, [rsp_text]]except Exception as e:print(获取网页的html信息异常, str(e))return [0, 999999, 获取网页的html信息异常, str(e), [None]]def spider_weather(region_code, tqyb_type):方法名称: 爬取天气预报信息中文注释: 根据地区代码天气预报类型爬取天气预报信息入参:param region_code str 地区代码param tqyb_type str 类型(1-今天2-近7天)出参:返回状态:return 0 失败或异常return 1 成功返回错误码返回错误信息param rsp_dict dict 响应容器作 者: PandaCode辉创建时间: 2023-09-05使用范例: spider_weather(101010100,1)天气预报网址http://www.weather.com.cn/weather/101010100.shtml --北京市近7日天气http://www.weather.com.cn/weather1d/101010100.shtml --北京市今天天气try:if (not type(region_code) is str):return [0, 111111, 地区代码参数类型错误,不为字符串, [None]]if (not type(tqyb_type) is str):return [0, 111112, 类型参数类型错误,不为字符串, [None]]url # 类型(1-今天2-近7天)if tqyb_type 1:url http://www.weather.com.cn/weather1d/ region_code .shtmlelif tqyb_type 2:url http://www.weather.com.cn/weather/ region_code .shtml# UTC格式当前时区时间t time.localtime()work_time time.strftime(%Y-%m-%d %H:%M:%S, t)print(当前日期时间: str(work_time))now_day str(work_time)[:7]# 根据url地址获取网页信息rst get_html_text(url)if rst[0] ! 1:return rsthtml_str rst[3][0]# 使用BeautifulSoup解析网页数据soup BeautifulSoup(html_str, html.parser)# 返回容器初始化rsp_dict {}# 类型(1-今天2-近7天)if tqyb_type 1:# 获取今天天气信息# 白天,天气情况tq_day_info soup.select(div.t ul.clearfix li p.wea)[0].textrsp_dict[tq_day_info] 白天,天气情况: tq_day_infoprint(rsp_dict[tq_day_info])# 最高温度temperatrue_high soup.select(div.t ul.clearfix li p.tem)[0].text# 去除换行符temperatrue_high .join(re.findall(r\S, temperatrue_high))rsp_dict[temperatrue_high] 白天,最高温度: temperatrue_highprint(rsp_dict[temperatrue_high])# 夜间,天气情况tq_night_info soup.select(div.t ul.clearfix li p.wea)[1].textrsp_dict[tq_night_info] 夜间,天气情况: tq_night_infoprint(rsp_dict[tq_night_info])# 夜间,最低温度temperatrue_low soup.select(div.t ul.clearfix li p.tem)[1].text# 去除换行符temperatrue_low .join(re.findall(r\S, temperatrue_low))rsp_dict[temperatrue_low] 夜间,最低温度: temperatrue_lowprint(rsp_dict[temperatrue_low])print()elif tqyb_type 2:# 获取近7日天气rsp_dict[c7day_list] []# 日期day_info soup.select(div.c7d input input input ul.t.clearfix li h1)# print(日期: str(day_info))# 天气tq_info soup.select(div.c7d input input input ul.t.clearfix li p.wea)# print(天气: str(tq_info))# 温度tem_info soup.select(div.c7d input input input ul.t.clearfix li p.tem)# print(温度: str(tem_info))# 风力win_info soup.select(div.c7d input input input ul.t.clearfix li p.win i)# print(风力: str(win_info))# 列表存储for i in range(7):temp_dict {}# 日期temp_dict[day_info] 日期: now_day - str(day_info[i].text)print(temp_dict[day_info])# 天气temp_dict[tq_info] 天气: str(tq_info[i].text)print(temp_dict[tq_info])# 温度# 去除换行符temperatrue .join(re.findall(r\S, str(tem_info[i].text)))temp_dict[tem_info] 温度: temperatrueprint(temp_dict[tem_info])# 风力temp_dict[win_info] 风力: str(win_info[i].text)print(temp_dict[win_info])# 添加到列表rsp_dict[c7day_list].append(temp_dict)print()# 返回容器return [1, 000000, 爬取天气预报信息成功, [rsp_dict]]except Exception as e:print(爬取天气预报信息异常, str(e))return [0, 999999, 爬取天气预报信息异常, str(e), [None]]# 主方法
if __name__ __main__:# 101010100 - 北京市# 爬取天气预报-今天rst1 spider_weather(101010100, 1)rsp_dict1 rst1[3][0]print(rsp_dict1)# 爬取天气预报-近7天rst2 spider_weather(101010100, 2)rsp_dict2 rst2[3][0]print(rsp_dict2)