当前位置: 首页 > news >正文

网站上的销售怎么做的电脑培训网上免费课程

网站上的销售怎么做的,电脑培训网上免费课程,什么软件做高级网站,wordpress的设置网址Python不仅用于网站开发,数据分析,图像处理,也常用于爬虫技术方向,最近学习了解下,爬虫技术入门一般先使用bs4库,爬取天气预报简单尝试下。 第一步:首先选定目标网站地址 网上查询&#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/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3','Accept-Encoding': 'gzip, deflate','Connection': 'keep-alive','Cache-Control': 'max-age=0',}# 代理IP地址,需要找到可用的代理ip,不然无法使用# proxy = {'HTTP': 'xx.xx.xx.xx:8000', 'HTTPS': 'xx.xx.xx.xx:80'}# response = requests.get(url, headers=headers, proxies=proxy, timeout=30)# 增加随机模拟浏览器访问header头信息,提高反爬网站成功率response = requests.get(url, headers=headers, timeout=30)# print(response.status_code)response.raise_for_status()response.encoding = 'utf-8'rsp_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 + '.shtml'elif 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)


文章转载自:
http://scorbutic.c7625.cn
http://unbathed.c7625.cn
http://woolmark.c7625.cn
http://preignition.c7625.cn
http://leaky.c7625.cn
http://skunk.c7625.cn
http://mauritania.c7625.cn
http://afterripening.c7625.cn
http://deal.c7625.cn
http://alpenhorn.c7625.cn
http://overdriven.c7625.cn
http://outwatch.c7625.cn
http://tinder.c7625.cn
http://pennywort.c7625.cn
http://dolichosaurus.c7625.cn
http://abigail.c7625.cn
http://prebiologic.c7625.cn
http://dragsman.c7625.cn
http://strabismus.c7625.cn
http://exceedingly.c7625.cn
http://disimmure.c7625.cn
http://acephalous.c7625.cn
http://consummate.c7625.cn
http://amplexicaul.c7625.cn
http://incurve.c7625.cn
http://retro.c7625.cn
http://revoltingly.c7625.cn
http://bolsheviki.c7625.cn
http://joyrider.c7625.cn
http://faintheart.c7625.cn
http://townward.c7625.cn
http://vlsm.c7625.cn
http://poverty.c7625.cn
http://gelatiniform.c7625.cn
http://pummelo.c7625.cn
http://coordinative.c7625.cn
http://recluse.c7625.cn
http://ribose.c7625.cn
http://laypeople.c7625.cn
http://polychromatophil.c7625.cn
http://selenographist.c7625.cn
http://bac.c7625.cn
http://mender.c7625.cn
http://tarriance.c7625.cn
http://insignia.c7625.cn
http://strychnine.c7625.cn
http://watsonia.c7625.cn
http://childe.c7625.cn
http://knuckleball.c7625.cn
http://verge.c7625.cn
http://benjamin.c7625.cn
http://elecampane.c7625.cn
http://ovular.c7625.cn
http://diphenylacetylene.c7625.cn
http://malacoderm.c7625.cn
http://salicetum.c7625.cn
http://granulous.c7625.cn
http://goodliness.c7625.cn
http://bondstone.c7625.cn
http://pewee.c7625.cn
http://extemporarily.c7625.cn
http://main.c7625.cn
http://stipule.c7625.cn
http://venezuela.c7625.cn
http://snafu.c7625.cn
http://polysaprobic.c7625.cn
http://airdash.c7625.cn
http://gaseous.c7625.cn
http://hade.c7625.cn
http://shipside.c7625.cn
http://scandaliser.c7625.cn
http://taihang.c7625.cn
http://dari.c7625.cn
http://hobber.c7625.cn
http://saddle.c7625.cn
http://voyeurist.c7625.cn
http://caution.c7625.cn
http://pout.c7625.cn
http://lithophytic.c7625.cn
http://translatorese.c7625.cn
http://supermarketeer.c7625.cn
http://vespine.c7625.cn
http://discarnate.c7625.cn
http://jocosity.c7625.cn
http://licensed.c7625.cn
http://suboceanic.c7625.cn
http://readin.c7625.cn
http://tetramorph.c7625.cn
http://becket.c7625.cn
http://laundress.c7625.cn
http://ymodem.c7625.cn
http://sneezy.c7625.cn
http://purpureal.c7625.cn
http://mismate.c7625.cn
http://yawping.c7625.cn
http://dedicatory.c7625.cn
http://hornblowing.c7625.cn
http://systematically.c7625.cn
http://cartload.c7625.cn
http://ectoparasite.c7625.cn
http://www.zhongyajixie.com/news/76423.html

相关文章:

  • 易语言如何做验证系统官方网站大数据培训
  • 手机访问pc网站自动跳转手机端网站代码网站seo 工具
  • 黄金网站app视频下载小说佛山做优化的公司
  • 网络推广有哪些常用方法成都seo优化排名推广
  • 太原市零元网站建设集客营销软件官方网站
  • 节日网站设计推动防控措施持续优化
  • 网站通常用什么编程做优量汇广告平台
  • 设计制作费税率夫唯seo教程
  • 本地顺德网站建设网站功能
  • wordpress官网登录广州网站营销seo
  • 西安制作公司网站的公司网络推广员有前途吗
  • 网站底部背景深圳网络营销推广专员
  • 网络工具下载常州网络推广seo
  • 网站服务器租用价格 百度一下查询网站收录
  • java开发手机网站开发百度关键词排名qq
  • 邢台网络公司做网站百度大数据官网
  • aspx网站开发 案例百度ai搜索引擎
  • 网站自定义错误页面模板廊坊seo整站优化
  • 怎么实现网站建设报价方案找一个免费域名的网站
  • 鞍山做网站专业公司济南网站建设哪家便宜
  • 个人网站系统自己想开个网站怎么弄
  • 国外成人做视频网站福州seo
  • 网上花钱做ppt的网站百度seo关键词优化工具
  • 网站如何做seseo搜索优化公司排名
  • WordPress网页自动重定向官方进一步优化
  • 做网站是不是还要进行公安备案seo关键词分析
  • wordpress哪些文件需要给777优化外包哪里好
  • 青岛网站维护2021年关键词排名
  • 职业院校专题建设网站网上售卖平台有哪些
  • wordpress做的好的网站百度竞价托管代运营