Python: คุณขูดข้อมูลรายวันจากเว็บไดนามิกโดยใช้ Python ได้อย่างไร

Aug 22 2020

รหัสต่อไปนี้ใช้งานได้ แต่จะหยุดหลังจากวันที่ 29 กุมภาพันธ์เว็บไซต์ส่งคืน "คุณป้อนวันที่ไม่ถูกต้องโปรดป้อนการค้นหาของคุณอีกครั้ง" ซึ่งจำเป็นต้องคลิกที่ "ตกลง" ฉันจะหลีกเลี่ยงสิ่งนี้ได้อย่างไร?

country_search("United States")
time.sleep(2)
date_select = Select(driver.find_element_by_name("dr")) 
date_select.select_by_visible_text("Enter date range...") #All Dates
select_economic_news()
#btnModifySearch
for month in range(1,9):
for day in range(1,32):
    try:
    
        set_from_month(month)
        set_from_date(day)
        set_from_year("2020")
        set_to_month(month)
        set_to_date(day)
        set_to_year("2020")
                
        time.sleep(5)
        #select_economic_news()
        time.sleep(5)
        search_now()
        time.sleep(8)                
                
        export_csv()
        modify_search()
        
        time.sleep(5)        
        #country_remove()
    except ElementClickInterceptedException:
        break

ออกจากระบบ()

คำตอบ

derringa Aug 22 2020 at 21:07

หากคุณสามารถใช้เฉพาะวิธีการที่แสดงในโพสต์เริ่มต้นฉันจะลองทำสิ่งต่อไปนี้:

set_from_year('2020')
set_to_year('2020')
for month in range(1, 9):
    # 1 to 9 for Jan to Aug
    month_str = '0' + str(month)
    set_from_month(month_str)
    set_to_month(month_str)
    for day in range(1, 32):
        # Assuming an error is thrown for invalid days
        try:
            # Store data as needed
        except Exception as e:
            # print(e) to learn from error if needed
            pass

มีอะไรอีกมากมายที่จะเข้าสู่สิ่งนี้หากปรากฎว่าคุณกำลังเขียนวิธีการเหล่านี้ด้วยตัวเองและต้องวนซ้ำ HTML และค้นหารูปแบบสำหรับข้อมูลรายวัน

ro_ot Aug 22 2020 at 21:22

ฉันเชื่อว่าคุณต้องการรับจำนวนวันในหนึ่งเดือนแบบไดนามิกเพื่อที่คุณจะได้วนซ้ำตัวเลขนั้นเพื่อรับข้อมูลสำหรับแต่ละวัน คุณสามารถทำได้ดังนี้:

from datetime import datetime
currentDay = datetime.today()
# You can set the currentDay using this if you want the data till the current date or 
# whenever your scheduler runs the job.


# Now you need to get the number of days in each month from the chosen date, you can 
# have the corresponding function like getStartMonth() in your program which will 
# return the starting month.  
from calendar import monthrange
daysPerMonth = {}
year = currentDay.year #TODO : change this to getStartYear()
startMonth = 3 # TODO : Implement getStartMonth() in your code.
for month in range(startMonth, currentDay.month+1):
    # monthrange returns (weekday,number of days in that month)
    daysPerMonth[month] = monthrange(year, month)[1]

for month in daysPerMonth.items(): 
    print(month[0], '-',month[1])

สิ่งนี้จะได้ผลลัพธ์ดังนี้ ( จำนวนวันในหนึ่งเดือนตั้งแต่ - มีนาคม 2020 ถึงสิงหาคม 2020 ):

3 - 31
4 - 30
5 - 31
6 - 30
7 - 31
8 - 31

จากนั้นคุณสามารถเรียกใช้ลูปเป็นเวลาหลายวันในขณะที่อ้างอิงช่วงจากคำสั่งที่คุณได้รับ หมายเหตุ: ในฟังก์ชันที่คุณกำลังเรียกใช้ลูปเพื่อรับข้อมูลสำหรับแต่ละวันให้เพิ่มหนึ่งถ้าเงื่อนไขเพื่อตรวจสอบว่าเป็นวันสุดท้ายของปีหรือไม่และแก้ไขปีตามนั้น

M.Liver Aug 23 2020 at 04:57

บางทีคุณสามารถใช้ฟังก์ชันเหล่านี้เพื่อนับวันในเดือน:

import datetime


def get_month_days_count(year: int, month: int) -> int:
    date = datetime.datetime(year, month, 1)
    while (date + datetime.timedelta(days=1)).month == month:
        date = date + datetime.timedelta(days=1)
    return date.day