整合營銷服務(wù)商

          電腦端+手機端+微信端=數(shù)據(jù)同步管理

          免費咨詢熱線:

          Vue前端框架:html指令

          Vue前端框架:html指令

          是講到v杠test和差值表示。下面再看一個,也就是v杠h,小娜就是h,t r 指定。看一下例子。

          比如現(xiàn)在還是例子ctrl c ctrl v幺五,bm-m l直接回看。

          下面看一個例子。

          ·比如我現(xiàn)在把立杠 casp定位通通改成h天亮,通通改成h天亮。來看一下mercy 的時候還是依然是所某的三姨太。

          ·對于 mercy來說它是不是這地方依然是所某的三姨太。

          ·對第二個來說the dress 來說這地方是不是四門標簽后背景一些死亡標簽?

          ·第三個皮卡丘聽到今天氣很不錯,又見學(xué)校。這個是這種事somo 的三姨太。會發(fā)現(xiàn)首先說we gone html和we gone test是一樣的,都會把標簽這種原有的內(nèi)容給清空。

          ·這時會發(fā)現(xiàn)v杠h q l會不會對這個標簽進行解釋?因為上是不是不會有死盤標線?讓右鍵去檢查的時候大家可以看一下有沒有死亡標簽。

          有,聽說會發(fā)現(xiàn)h顯標指令其實是會對標簽進行演示的。這就是test指令。這就是httl 指令,什么指令我就寫在這。

          取數(shù)據(jù)并寫入xls文件

          import csv
          import random
          
          import requests
          from lxml import etree
          
          # 城市列表如下:
          # http://hebei.weather.com.cn/m2/j/hebei/public/city.min.js
          # 目前支持北京、天津、重慶三個城市7天天氣預(yù)報
          # 支持河南天氣更新
          # 18點后獲取天氣預(yù)報將get_text()方法中的0改為1
          headers=[
              "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36",
              "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 "
              "Safari/537.36",
              "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0",
              "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 "
              "Safari/537.75.14",
              "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)",
              'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
              '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 "]
          
          
          def get_province():
              url='http://www.weather.com.cn/province/'
              r=requests.get(url, headers={'User-Agent': headers[random.randint(1, 11)]})
              # encode解碼,將ISO-8859-1解碼成unicode
              html=r.text.encode("ISO-8859-1")
              # decode編碼,將unicode編碼成utf-8
              html=html.decode("utf-8")
              html1=etree.HTML(html)
              data=html1.xpath('/html/body/div[2]/div[2]/ul/li/a')
              list_province=[]
              for i in data:
                  item={'省轄市': i.text, '鏈接': i.get('href')}
                  list_province.append(item)
              return list_province
          
          
          def get_city_link(ul, ulink, list_weather):
              ul=ul
              ulink=ulink
              if ul in list_weather:
                  url=ulink
                  r=requests.get(url, headers={'User-Agent': headers[random.randint(1, 11)]})
                  # encode解碼,將ISO-8859-1解碼成unicode
                  html=r.text.encode("ISO-8859-1")
                  # decode編碼,將unicode編碼成utf-8
                  html=html.decode("utf-8")
                  html1=etree.HTML(html)
                  return html1
              else:
                  pass
          
          
          def get_special(ulink):
              url=ulink
              r=requests.get(url, headers={'User-Agent': headers[random.randint(1, 11)]})
              # encode解碼,將ISO-8859-1解碼成unicode
              html=r.text.encode("ISO-8859-1")
              # decode編碼,將unicode編碼成utf-8
              html=html.decode("utf-8")
              html1=etree.HTML(html)
              return html1
          
          
          def get_city(list_):
              # 上海天氣10月23日網(wǎng)頁改版
              list_all=['北京', '天津', '重慶']
              list_null=['山西', '湖北', '青海']
              # 安徽  http://www.weather.com.cn/anhui/index.shtml
              # 完整url
              # /html/body/div[1]/div[3]/div/span/a[1]
              # 廣東
              # /html/body/div[2]/ul/li[6]/a
              # 廣西
              # /html/body/div[1]/div[1]/div[2]/div/span/a[4]
              # 黑龍江
              # /html/body/div[3]/div/a[4]
              list_special_city=['臺灣', '香港', '澳門', '河北']
              list_http=['河南', '山東', '陜西', '江蘇', '湖南', '福建', '海南', '云南', '四川', '西藏', '江西', '新疆', '甘肅', '寧夏', '內(nèi)蒙古', '吉林',
                           '遼寧']
              list_city=[]
              for i in list_:
                  ul=i['省轄市']
                  ulink=i['鏈接']
                  if ul in list_all:
                      html=get_city_link(ul, ulink, list_all)
                      data=html.xpath('/html[1]/body[1]/div[1]/div[2]/div[1]/span[1]/a')
                      for i in data:
                          item={'市,區(qū)': i.text, '鏈接': i.get('href')}
                          list_city.append(item)
                  if ul in list_http:
                      html1=get_city_link(ul, ulink, list_http)
                      data1=html1.xpath('/html/body/div[1]/div[2]/div/span/a')
                      for i in data1:
                          item={'市,區(qū)': i.text, '鏈接': (ulink + i.get('href'))}
                          list_city.append(item)
                  if ul in list_null:
                      html2=get_city_link(ul, ulink, list_null)
                      data2=html2.xpath('/html/body/div[2]/div[2]/div/span/a')
                      for i in data2:
                          item={'市,區(qū)': i.text, '鏈接': (ulink + i.get('href'))}
                          list_city.append(item)
                  if ul in list_special_city:
                      pass
                  if ul=='安徽':
                      html=get_special(' http://www.weather.com.cn/anhui/index.shtml')
                      data=html.xpath('/html/body/div[1]/div[3]/div/span/a')
                      for i in data:
                          item={'市,區(qū)': i.text, '鏈接': i.get('href')}
                          list_city.append(item)
                  if ul=='廣東':
                      html=get_special(ulink)
                      data=html.xpath(' /html/body/div[2]/ul/li[6]/a')
                      for i in data:
                          item={'市,區(qū)': i.text, '鏈接': (ulink + i.get('href'))}
                          list_city.append(item)
                  if ul=='廣西':
                      html=get_special(ulink)
                      data=html.xpath('/html/body/div[1]/div[1]/div[2]/div/span/a')
                      for i in data:
                          item={'市,區(qū)': i.text, '鏈接': (ulink + i.get('href'))}
                          list_city.append(item)
                  if ul=='黑龍江':
                      html=get_special(ulink)
                      data=html.xpath('/html/body/div[3]/div/a')
                      for i in data:
                          item={'市,區(qū)': i.text, '鏈接': (ulink + i.get('href'))}
                          list_city.append(item)
              return list_city
          
          
          # 北京、天津、重慶
          def get_weather():
              # 風(fēng)向僅供參考
              All_url=get_city(get_province())
              list_weather=[]
              for i in All_url:
                  url=i['鏈接']
                  name=i['市,區(qū)']
                  r=requests.get(url, headers={'User-Agent': headers[random.randint(1, 11)]})
                  # encode解碼,將ISO-8859-1解碼成unicode
                  html=r.text.encode("ISO-8859-1")
                  # decode編碼,將unicode編碼成utf-8
                  html=html.decode("utf-8")
                  html1=etree.HTML(html)
                  data_time=html1.xpath('//div[@class="con today clearfix"]//ul[@class="t clearfix"]//li//h1')
                  data_weather=html1.xpath('//div[@class="con today clearfix"]//ul[@class="t clearfix"]//li//p[@class="wea"]')
                  data_temperature=html1.xpath(
                      '//div[@class="con today clearfix"]//ul[@class="t clearfix"]//li//p[@class="tem"]//i')
                  data_wind_level=html1.xpath(
                      '//div[@class="con today clearfix"]//ul[@class="t clearfix"]//li//p[@class="win"]//i')
                  data_wind=html1.xpath(
                      '//div[@class="con today clearfix"]//ul[@class="t clearfix"]//li//p[@class="win"]//em//span')
                  for i in range(0, len(data_time)):
                      Item={'城市': name,
                              '時間': data_time[i].text,
                              '天氣': data_weather[i].text,
                              '溫度': data_temperature[i].text,
                              '風(fēng)力': data_wind_level[i].text,
                              '風(fēng)向': data_wind[i].get('title')}
                      list_weather.append(Item)
              csv_File=open("D:\\beijing_tianjin_chongqing_weather.csv", 'w', newline='')
              try:
                  writer=csv.writer(csv_File)
                  writer.writerow(('城市', '時間', '天氣', '實時溫度', '風(fēng)力', '風(fēng)向'))
                  for i in list_weather:
                      writer.writerow((i['城市'], i['時間'], i['天氣'], i['溫度'], i['風(fēng)力'], i['風(fēng)向']))
              finally:
                  csv_File.close()
              print('北京,重慶,天津天氣獲取成功')
          
          
          def get_henan():
              All_url=get_city(get_province())
              list_henan=['鄭州', '安陽', '濮陽', '鶴壁', '焦作', '濟源', '新鄉(xiāng)', '三門峽', '洛陽', '平頂山', '許昌', '漯河', '開封', '周口', '商丘', '南陽',
                            '信陽', '駐馬店']
              list_weather1=[]
              for i in All_url:
                  url=i['鏈接']
                  name=i['市,區(qū)']
                  if name in list_henan:
                      url=url
                      r=requests.get(url, headers={
                          'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, '
                                        'like Gecko) Chrome/78.0.3904.70 Safari/537.36'})
                      # encode解碼,將ISO-8859-1解碼成unicode
                      html=r.text.encode("ISO-8859-1")
                      # decode編碼,將unicode編碼成utf-8
                      html=html.decode("utf-8")
                      html1=etree.HTML(html)
                      url_true=html1.xpath('//div[@class="gsbox"]//div[@class="forecastBox"]//dl//dt//a[1]')
                      Item={
                          '城市': name,
                          '鏈接': url_true[0].get('href')
                      }
                      list_weather1.append(Item)
              return list_weather1
          
          
          def get_text():
              list_weather1=[]
              list_weather=get_henan()
              for i in list_weather:
                  url=i['鏈接']
                  name=i['城市']
                  r=requests.get(url,
                                   headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, '
                                                          'like Gecko) Chrome/78.0.3904.70 Safari/537.36'})
                  # encode解碼,將ISO-8859-1解碼成unicode
                  html=r.text.encode("ISO-8859-1")
                  # decode編碼,將unicode編碼成utf-8
                  html=html.decode("utf-8")
                  html1=etree.HTML(html)
                  data_time=html1.xpath('//div[@class="left fl"]//ul//li//h1')
                  data_high_temperature=html1.xpath('//div[@class="left fl"]//ul//li//p[@class="tem"]//span')
                  data_low_temperature=html1.xpath('//div[@class="left fl"]//ul//p[@class="tem"]//i')
                  data_wind=html1.xpath('//div[@class="left fl"]//ul//li//p[@class="win"]//em//span')  # 獲取title標簽
                  data_wind_level=html1.xpath('//div[@class="left fl"]//ul//li//p[@class="win"]//i')
                  data_weather=html1.xpath('//div[@class="left fl"]//ul//li//p[@class="wea"]')  # 獲取title標簽
                  for i in range(0, len(data_time)):
                      Item={'城市': name,
                              '時間': data_time[i].text,
                              '天氣': data_weather[i].get('title'),
                              '高溫': data_high_temperature[i - 1].text,
                              '低溫': data_low_temperature[i].text,
                              '風(fēng)向': data_wind[i].get('title'),
                              '風(fēng)力': data_wind_level[i].text
                              }
                      list_weather1.append(Item)
              csv_File=open("D:\\henan_weather.csv", 'w', newline='')
              try:
                  writer=csv.writer(csv_File)
                  writer.writerow(('城市', '時間', '天氣', '高溫', '低溫', '風(fēng)力', '風(fēng)向'))
                  for i in list_weather1:
                      writer.writerow((i['城市'], i['時間'], i['天氣'], i['高溫'], i['低溫'], i['風(fēng)力'], i['風(fēng)向']))
              finally:
                  csv_File.close()
              print('河南天氣獲取成功')
          
          
          if __name__=='__main__':
              get_text()
              get_weather()
          
          

          寫入數(shù)據(jù)庫

          近看了一下《Web Scraping with Python_ Collecting Data from the Modern Web》這本書,所以得動手實踐一下,故做此文,與大家分享,希望共同進步

          準備工作:

          1.安裝模塊: pandas, BeautifulSoup

          2. 注冊 mailgun 賬號 (https://www.mailgun.com/email-api),用來發(fā)郵件

          step1: 點擊SIGN UP 注冊mailgun賬號,記住 不要勾選 Add payment info now

          step2: 當注冊完成之后,記住勾選下面的Python 欄, 復(fù)制下面的code到spyder中去

          3. 當上述準備工作完成之后:導(dǎo)入需要用到的模塊:

          from urllib.request import urlopen ,Request
          from bs4 import BeautifulSoup
          import re
          import pandas as pd
          import time
          import requests
          

          定義function send_simple_message : 主要功能為利用mailgun api 推送郵件

          #將復(fù)制出來的代碼進行細微的修改,比如自定義 subject 和 text 參數(shù)
          def send_simple_message(subject, text):
           return requests.post(
           "https://api.mailgun.net/v3/sandbox3acbbfebb26a46e685e3561ea38876b3.mailgun.org/messages",
           auth=("api", "fd8db37ccb171b1048e86ffc6f3645fd-b892f62e-0a41511c"),
           data={"from": "Mailgun Sandbox <postmaster@sandbox3acbbfebb26a46e685e3561ea38876b3.mailgun.org>",
           # 目標郵箱
           "to": "jin zhu <zhujin19@qq.com>", 
           "subject": subject,
           "text":text })
          

          4. 利用 BeautifulSoup , Request 庫抓取天氣信息,我們這里以北京地區(qū)的信息為例:

          【北京天氣】北京天氣預(yù)報,藍天,藍天預(yù)報,霧霾,霧霾消散,天氣預(yù)報一周,天氣預(yù)報15天查詢

          ?

          www.weather.com.cn

          def get_weather_info(chaper_url='http://www.weather.com.cn/weather/101010100.shtml'):
           
           headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'} 
           req=Request(url=chaper_url, headers=headers) 
           html=urlopen(req).read() 
           bsObj=BeautifulSoup(html,'html.parser')
           weather=bsObj.find_all(class_='wea')
           temp=bsObj.find_all(class_='tem')
           win=bsObj.find_all(class_='win')
           date=bsObj.find("div", {'id': '7d'}).find('ul').find_all('li')
           data=pd.DataFrame(columns=['日期', '天氣', '溫度', '風(fēng)級'],index=[])
           for i in range(len(weather)): 
           data.loc[i,'日期']=date[i].find('h1').string
           data.loc[i,'天氣']=weather[i].get_text()
           data.loc[i,'溫度']=''.join(temp[i].get_text().split())
           data.loc[i,'風(fēng)級']=''.join(win[i].get_text().split())
           
           data.set_index(['日期'], inplace=True) 
           data_final=data.to_csv().split()
           return data_final
          

          可以通過單擊 右鍵查看中國天氣網(wǎng)的源代碼: 可知<p class="wea">,<p class="tem">,<p class="win"> 分別為 天氣, 溫度,風(fēng)級的標簽。 上述code主要是分開抓取 這些標簽,然后將它們存放為dataframe data里面保存

          5. 定時執(zhí)行上述code 并發(fā)送郵件到指定郵箱: 這里面是每隔5分鐘執(zhí)行一次上述程序,如果希望每24小時執(zhí)行一次上述code,請將 300 改為 86400即可。

          while True:
           subject='北京未來7天氣預(yù)報'+time.strftime('%Y-%m-%d',time.localtime(time.time()))
           send_simple_message(subject,get_weather_info())
          # 每次間隔5分鐘執(zhí)行一次code
           time.sleep(300.0 - time.time()% 300.0)
          # time.sleep(86400 - time.time()% 86400.0)
          

          這里可以參考以下鏈接:

          What is the best way to repeatedly execute a function every x seconds in Python?

          來 定時執(zhí)行 code

          最終呈現(xiàn)的效果如下圖所示:

          最后,如果大家想看其它地區(qū)的天氣預(yù)報,可以通過修改 get_weather_info 里面的 chaper_url 參數(shù), 也就是下面標粗的部分

          (chaper_url='http://www.weather.com.cn/weather/101010100.shtml')

          具體的城市代碼可以通過查看:

          https://max.book118.com/html/2016/0917/54858143.shtm

          歡迎大家關(guān)注知乎專欄:小草學(xué)python


          主站蜘蛛池模板: 国产激情一区二区三区四区 | 亚洲视频一区在线观看| 亚洲av无码不卡一区二区三区| 精品一区二区三区| 国产精品一级香蕉一区| 日本欧洲视频一区| 中文字幕精品无码一区二区| 一区二区三区免费在线视频| 无码少妇一区二区三区芒果| 国产精品亚洲午夜一区二区三区| 国产精品高清一区二区三区| 成人免费一区二区三区| 国产香蕉一区二区三区在线视频| 日韩精品无码一区二区视频| 亚洲毛片不卡av在线播放一区| 国产精品日韩一区二区三区 | 国产一区中文字幕在线观看 | 日韩精品一区二区三区中文字幕| 无码少妇丰满熟妇一区二区| 亚洲国产高清在线精品一区| 久久久久人妻一区精品性色av| 亚洲日韩精品一区二区三区无码| 无人码一区二区三区视频| 中文日韩字幕一区在线观看| 成人无码一区二区三区| 国产在线aaa片一区二区99| 三上悠亚一区二区观看| 中文字幕aⅴ人妻一区二区 | 国产在线步兵一区二区三区| 精品无码人妻一区二区三区18| 国产日韩精品视频一区二区三区| 色妞色视频一区二区三区四区| 蜜臀AV在线播放一区二区三区| 精品国产福利一区二区| 末成年女A∨片一区二区| 亚洲视频一区网站| 激情综合丝袜美女一区二区 | 亚欧在线精品免费观看一区| 99久久精品日本一区二区免费| 精品国产乱子伦一区二区三区| 成人毛片无码一区二区|