首页 > 其他分享 >【爬虫】项目篇-selenium爬取大鱼潮汐网

【爬虫】项目篇-selenium爬取大鱼潮汐网

时间:2024-04-05 23:46:45浏览次数:14  
标签:xpath get selenium 爬虫 爬取 print drvier date find

爬取指定日期的潮汐数据

  1. 创建driver对象,并设为最大窗口
url="https://www.chaoxibiao.net/tides/75.html"
option=Options()
option.binary_location=r"C:\Users\txmmy\AppData\Local\Google\Chrome\Application\chrome.exe"
drvier=webdriver.Chrome(options=option)
drvier.get(url)
drvier.maximize_window()

2.设置起始日期和终止日期

date1='2003-1-1'
date2='2003-3-1'

3.由于需要自动切换日期,因此需要设置自动切换日期的方法。首先是定位到年份,其次定位到月份

def locate_date():
	# 将字符串日期改为日期格式
	date1_date = datetime.strptime(date1, "%Y-%m-%d").date()
	#获取起始年份
	y1 = int(date1.split('-')[0])
	#点击切换日期按钮
	drvier.find_element_by_xpath('//*[@id="changeDate"]').click()
	# 先选择年份
	drvier.find_element_by_xpath('//div[@class="laydate-set-ym"]/span[1]').click()
	#获取15个年份中的第一个,即最小年份
	init_year = drvier.find_element_by_xpath('//div[@class="layui-laydate-content"]/ul/li').get_attribute('lay-ym')
	#如果起始年份小于日历的最小年份,那就向前反也
    while y1 < int(init_year):
        # 向前翻页
        drvier.find_element_by_xpath('//*[@id="layui-laydate1"]/div[1]/div[1]/i[1]').click()
        # 得到当前日历的初始年份和末尾年份
        init_year = drvier.find_element_by_xpath('//div[@class="layui-laydate-content"]/ul/li').get_attribute(
            'lay-ym')
        last_year = drvier.find_element_by_xpath(
            '//div[@class="layui-laydate-content"]/ul/li/following-sibling::*[14]').get_attribute('lay-ym')
		# 如果起始年份大于等于当前日历页的最小年份且小于等于当前日历页的最大年份,则开始遍历当前日历页,最后点击
        if y1 >= int(init_year) and y1 <= int(last_year):
            for i in range(1, 15):
                year = drvier.find_element_by_xpath(
                    '//div[@class="layui-laydate-content"]/ul/li/following-sibling::*[%d]' % i)
                if y1 == int(year.get_attribute('lay-ym')):
                    year.click()
                    break
    # 定位月份
    m1 = int(date1.split('-')[1])
    drvier.find_element_by_xpath('//div[@class="laydate-set-ym"]/span[2]').click()
    time.sleep(2)
    init_m1 = drvier.find_element_by_xpath('//ul[@class="layui-laydate-list laydate-month-list"]/li[%d]' % m1)
    init_m1.click()
	
	# 点击月份后的日历页是一个6*7的表格,循环这个表格,获取当前日期。判断当前日期是否大于起始日期,如果当前日期为终止日期,则停止切换日期(循环),如果当前日期不为终止日期且为选中日期或当月日期,则点击选中,如果当前日期不为终止日期且为下个月日期,递归该函数。
    # 自动切换日期
    def change_date():
        for i in range(1, 7):
            for j in range(1, 8):
                a = drvier.find_element_by_xpath(
                    '//div[@class="layui-laydate-content"]/table/tbody/tr[%d]/td[%d]' % (i, j))
                # 字符串转换
                a_date = datetime.strptime(a.get_attribute('lay-ymd'), "%Y-%m-%d").date()
                print(a_date, date1_date)
                distance = a_date - date1_date
                if distance.days >= 0:
                    print(a.text)
                    print(a.get_attribute('lay-ymd'))
                    print("distance", distance.days)
                    # 若切换的日期为终止日期,停止循环
                    if a.get_attribute('lay-ymd') == date2:
                        a.click()
                        GetCondition()
                        return
                    else:
                        if a.get_attribute('class') == '' or a.get_attribute('class') == 'layui-this':
                            a.click()
                            time.sleep(2)
                            GetCondition()
                            drvier.find_element_by_xpath('//*[@id="changeDate"]').click()
                            time.sleep(2)

                        else:
                            if a.get_attribute('class') == 'laydate-day-next' and a.text == '1':
                                print("1", a.get_attribute('lay-ymd'))
                                a.click()
                                time.sleep(2)
                                drvier.find_element_by_xpath('//*[@id="changeDate"]').click()
                                return change_date()

    change_date()

定义GetCondition获取农历、公历、潮汐级别image

    def GetCondition():
        # 获取公历
        date_lst = drvier.find_elements_by_xpath("//p/strong")
        date = ''.join([i.text for i in date_lst])

        # 获取农历
        lu_date_lst = drvier.find_elements_by_xpath('//p[@style="text-align:center;margin-bottom:0"]')
        lu_date = [i.text for i in lu_date_lst][0].split(" ")[1][:-2]

        # 获取潮汐情况
        tide_lst = drvier.find_elements_by_xpath('//p/span[@class="cnTides"]')
        tide = ''.join([i.text for i in tide_lst])
        print("公历:", date, " 农历:", lu_date, " 潮汐情况:", tide)

4.使用动作链ActionChains移动坐标来获取24小时的潮高和时间,这里使用谷歌浏览器插件Page Ruler Redux来获取坐标

    list={}
    try:
        #定位锚点,获取24小时的潮高
        for i in range(1, 25):
            action = ActionChains(drvier)
            if i==1:
                action.move_by_offset(373,650).perform()
            elif i==24:
                action.move_by_offset(28,0).perform()
            else:
                action.move_by_offset(34,0).perform()
            time.sleep(2)
            select = drvier.find_elements_by_xpath('//div[@id="tidesLine"]')
            for i in select:
                txt= i.text
                txt=re.sub('\n',"",txt)
                list['time']=re.search('时间 : (.*?)潮高',txt).group(1)
                list['high']=re.search('潮高 : (.+)',txt).group(1)
                print(type(txt))
                print(txt)
                print(list)
            time.sleep(2)
    except Exception as e:
        print(e.args)

完整代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
import time
import re
from datetime import  datetime
import csv
import os
url="https://www.chaoxibiao.net/tides/75.html"
def locate_date():
    print("date2", date2)
    date1_date = datetime.strptime(date1, "%Y-%m-%d").date()
    y1 = int(date1.split('-')[0])
    # 点击切换日期按钮 2003-01-01
    drvier.find_element_by_xpath('//*[@id="changeDate"]').click()

    # 点击2021年,开始选择年份
    drvier.find_element_by_xpath('//div[@class="laydate-set-ym"]/span[1]').click()

    # 获取15个年份中第一个,即最小的年份
    init_year = drvier.find_element_by_xpath('//div[@class="layui-laydate-content"]/ul/li').get_attribute('lay-ym')
    # 如果y小于日历的最小年份
    while y1 < int(init_year):
        # 向前翻页
        drvier.find_element_by_xpath('//*[@id="layui-laydate1"]/div[1]/div[1]/i[1]').click()
        # 得到当前日历的初始年份和末尾年份
        init_year = drvier.find_element_by_xpath('//div[@class="layui-laydate-content"]/ul/li').get_attribute(
            'lay-ym')
        last_year = drvier.find_element_by_xpath(
            '//div[@class="layui-laydate-content"]/ul/li/following-sibling::*[14]').get_attribute('lay-ym')
        if y1 >= int(init_year) and y1 <= int(last_year):
            for i in range(1, 15):
                year = drvier.find_element_by_xpath(
                    '//div[@class="layui-laydate-content"]/ul/li/following-sibling::*[%d]' % i)
                if y1 == int(year.get_attribute('lay-ym')):
                    year.click()
                    break
    time.sleep(2)

    # 定位月份
    m1 = int(date1.split('-')[1])
    drvier.find_element_by_xpath('//div[@class="laydate-set-ym"]/span[2]').click()
    time.sleep(2)
    init_m1 = drvier.find_element_by_xpath('//ul[@class="layui-laydate-list laydate-month-list"]/li[%d]' % m1)
    init_m1.click()

    # 自动切换日期
    def change_date():
        for i in range(1, 7):
            for j in range(1, 8):
                a = drvier.find_element_by_xpath(
                    '//div[@class="layui-laydate-content"]/table/tbody/tr[%d]/td[%d]' % (i, j))
                # 字符串转换
                a_date = datetime.strptime(a.get_attribute('lay-ymd'), "%Y-%m-%d").date()
                print(a_date, date1_date)
                distance = a_date - date1_date
                if distance.days >= 0:
                    print(a.text)
                    print(a.get_attribute('lay-ymd'))
                    print("distance", distance.days)
                    # 若切换的日期为终止日期,停止循环
                    if a.get_attribute('lay-ymd') == date2:
                        a.click()
                        GetCondition()
                        return
                    else:
                        if a.get_attribute('class') == '' or a.get_attribute('class') == 'layui-this':
                            a.click()
                            time.sleep(2)
                            GetCondition()
                            drvier.find_element_by_xpath('//*[@id="changeDate"]').click()
                            time.sleep(2)

                        else:
                            if a.get_attribute('class') == 'laydate-day-next' and a.text == '1':
                                print("1", a.get_attribute('lay-ymd'))
                                a.click()
                                time.sleep(2)
                                drvier.find_element_by_xpath('//*[@id="changeDate"]').click()
                                return change_date()

    change_date()

    def GetCondition():
        # 获取公历
        date_lst = drvier.find_elements_by_xpath("//p/strong")
        date = ''.join([i.text for i in date_lst])

        # 获取农历
        lu_date_lst = drvier.find_elements_by_xpath('//p[@style="text-align:center;margin-bottom:0"]')
        lu_date = [i.text for i in lu_date_lst][0].split(" ")[1][:-2]

        # 获取潮汐情况
        tide_lst = drvier.find_elements_by_xpath('//p/span[@class="cnTides"]')
        tide = ''.join([i.text for i in tide_lst])
        print("公历:", date, " 农历:", lu_date, " 潮汐情况:", tide)

def start():
    # 选择日期
    locate_date()
    list={}
    try:
        #定位锚点,获取24小时的潮高
        for i in range(1, 25):
            action = ActionChains(drvier)
            if i==1:
                action.move_by_offset(373,650).perform()
            elif i==24:
                action.move_by_offset(28,0).perform()
            else:
                action.move_by_offset(34,0).perform()
            time.sleep(2)
            select = drvier.find_elements_by_xpath('//div[@id="tidesLine"]')
            for i in select:
                txt= i.text
                txt=re.sub('\n',"",txt)
                list['time']=re.search('时间 : (.*?)潮高',txt).group(1)
                list['high']=re.search('潮高 : (.+)',txt).group(1)
                print(type(txt))
                print(txt)
                print(list)
            time.sleep(2)
    except Exception as e:
        print(e.args)

def save():
    pass
if __name__ == '__main__':

    option=Options()
    option.binary_location=r"C:\Users\txmmy\AppData\Local\Google\Chrome\Application\chrome.exe"
    drvier=webdriver.Chrome(options=option)
    drvier.get(url)
    drvier.maximize_window()
    time.sleep(2)
    date1='2003-1-1'
    date2='2003-3-1'

    #创建一个csv文件
    # if os.path.exists("-潮汐表.csv")==True:
    #     pass
    # else:
    #     newfile = open('.csv', 'w', newline='')

    start()


标签:xpath,get,selenium,爬虫,爬取,print,drvier,date,find
From: https://www.cnblogs.com/Gimm/p/18116744

相关文章

  • 【爬虫】项目篇-新东方六级听力音频
    importrequests,time,randomfromfake_useragentimportUserAgenturls=open(r'E:\01pycharmproject\网络爬虫技术\sjj1.txt',encoding='utf-8').read().split()i=1forurlinurls:headers={#'User-agent':'Mozilla/5......
  • 【爬虫】项目篇-使用selenium、requests爬取天猫商品评论
    目录使用selenium使用requests使用seleniumfromselenium.webdriverimportChrome,ChromeOptionsfromselenium.webdriver.support.waitimportWebDriverWaitfromselenium.webdriver.common.byimportByfromselenium.webdriver.supportimportexpected_conditionsasE......
  • 【爬虫】项目篇-爬取豆瓣电影周榜Top10,保存至Redis
    写法一:编写两个爬虫程序文件:爬虫1将豆瓣一周口碑榜的电影url添加到redis中名为movie_url的列表中(注意避免多次运行导致重复的问题);爬虫2从movie_url中读出网址,爬取每一部电影的导演、主演、类型、制片国家/地区、语言、上映日期、片长,并将它们保存到redis的hash表(自行命名)中。d......
  • 【爬虫】项目篇-在https://www.kanunu8.com/book2抓取电子书
    目录1)使用正则表达式2)使用bs41)使用正则表达式#使用requests库和正则表达式抓取在https://www.kanunu8.com/book3/任选的一本电子书importrequestsimportreimportosimporttimeheader={'user-agent':"Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit......
  • 【爬虫】项目篇-豆瓣读书Top250(https://book.douban.com/top250)
    抓取豆瓣读书Top250(https://book.douban.com/top250)每本书的书名、作者、出版社、出版时间、价格、评分等数据,将结果分别保存为csv文件和excel文件importxlwtimportxlsxwriterimportreimportrequestsfromfake_useragentimportUserAgentimportcchardetimporttime......
  • 【爬虫】项目篇-爬取丁香园的疫情数据
    ```#编写程序,从丁香园获取国内近期疫情数据,按省份提取当前确诊数,#确诊总数,疑似病例数,治愈数,死亡数,高危数等数据,保存到csv文件或excel文件中。importrequestsimportxlsxwriterfromfake_useragentimportUserAgentimportcchardetimportreimportjsonfrombs4importBeautif......
  • 【爬虫】项目篇-爬取福州公交线路并保存至MongoDB
    #http://www.fz-bus.cn/index.asp#1)在MongoDB中创建一个数据库和一个集合。#2)在程序执行过程中可输入线路名称查询公交线路,#每查询到一条线路的信息后,查询MongoDB数据库中是否存在该线路。若存在,则不做任何操作,否则执行第3步。#将线路名称、起点和终点、途径站点、#冬季首......
  • 【爬虫】debug篇-关于fake_useragent无法使用:Error occurred during loading data. Tr
    Erroroccurredduringloadingdata.Tryingtousecacheserverhttps://fake-useragent.herokuapp.com/browsers/0.1.11Traceback(mostrecentcalllast):File"D:\python\lib\site-packages\fake_useragent\utils.py",line154,inloadfori......
  • 【爬虫】项目篇-爬取豆瓣电影周榜
    目录使用re爬取+为请求头,保存为csv使用re爬取2+不保存使用xpath+lxml.html+lxml.etree使用re爬取+为请求头,保存为csvimportrequestsimportreimportcsvfromfake_useragentimportUserAgent#re文档:#https://docs.python.org/zh-cn/3.8/library/re.html#re.Sheader=......
  • selenium框架之无头浏览器
    一、无头浏览器介绍无头浏览器(HeadlessBrowser)是一种没有图形用户界面(GUI)的网络浏览器,它可以在后台运行并执行网页操作,而不需要打开一个可见的浏览器窗口。无头浏览器可以模拟用户在浏览器中执行的各种操作,例如加载网页、点击链接、填写表单等,但所有这些操作都在不可见的情况下......