Day---03 例子:爬取豌豆荚游戏页面信息

时间:2022-09-18 18:03:32
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time

def get_good(driver,number):
    try:
        time.sleep(5)
        js_code = '''
                        window.scrollTo(0,2500)
        '''
        driver.execute_script(js_code)

        time.sleep(5)

        good_list = driver.find_elements_by_class_name('card')

        for good in good_list:

            good_name = good.find_element_by_class_name('app-title-h2').text
            good_url =  good.find_element_by_css_selector('.app-title-h2 a').get_attribute('href')
            good_people_size = good.find_element_by_class_name('meta').text

            good_content = f'''
            游戏序号:{number}
            游戏名称:{good_name}
            游戏链接:{good_url}
            游戏下载人数_游戏大小:{good_people_size}
            \n
            '''

            print(good_content)

            with open('wandoujia.text','a',encoding='utf-8') as f:
                f.write(good_content)
            number+=1

        print("游戏信息写入成功!!")

        more_load=driver.find_element_by_class_name('load-more')
        more_load.click()

        time.sleep(5)

        get_good(driver,number)

    finally:
        driver.close()


if __name__ == '__main__':
    driver=webdriver.Chrome()


    try:
        num=1
        driver.implicitly_wait(5)
        driver.get('https://www.wandoujia.com/category/6001')
        get_good(driver,num)

        
    finally:
        driver.close()