|
python代码,但是每次读到89页就没有数据,报错退出......不知问题出在哪里,看来水平还是太菜了
- # -*- coding: utf-8 -*-
- from selenium import webdriver
- from time import sleep
- import csv
- driver = webdriver.Chrome()
- url = 'http://www.icourse163.org/category/all'
- driver.get(url)
- fields = ['编号','课程名称', '开课单位', '网址链接']
- sleep(5) #打开时页面底部有广告遮挡"下一页",留时间点掉
- m = 0
- with open('课程列表.csv','w', newline='') as f:
- wrt = csv.DictWriter(f, fieldnames=fields)
- wrt.writeheader()
- while True:
- sleep(0.5)
- div_list = driver.find_elements_by_xpath('//div[@class="u-clist f-bgw f-cb f-pr j-href ga-click"]')
- n = 0 #每一页的序号从1开始,所以换页后n重置为0
- for div in div_list:
- item = {}
- n += 1
- item['编号'] = n
- item['课程名称'] = div.find_element_by_xpath('.//div[@class="t1 f-f0 f-cb first-row"]/a').text
- item['开课单位'] = div.find_element_by_xpath('.//div[@class="t2 f-fc3 f-nowrp f-f0"]/a').text
- item['网址链接'] = div.find_element_by_xpath('.//div[@class="t1 f-f0 f-cb first-row"]/a').get_attribute('href')
- wrt.writerow(item)
- m += 1
- #点击下面页码无法导航到最后一页,找不到最后一个'下一页'的class,只好用m值来判断是否读取所有页面
- if m > 155:
- break
- #点击'下一页'
- driver.find_element_by_xpath('//li[@class="ux-pager_btn ux-pager_btn__next"]').click()
复制代码 |
评分
-
1
查看全部评分
-
|