# _*_ coding:utf-8 _*_
# @Time : -5-9 15:42
# @Author : BGLB
# @Software : PyCharm
from time import sleep
from selenium.webdriver import Chrome
web = Chrome()
web.maximize_window()
def get_element():
web.get("https://www.python123.io/index/login")
uername = web.find_element_by_xpath(
'//*[@id="links"]/div[1]/div[2]/div/div[1]/div[1]/div/div[1]/div/div[1]/form/div[1]/div/input')
passworld = web.find_element_by_xpath(
'//*[@id="links"]/div[1]/div[2]/div/div[1]/div[1]/div/div[1]/div/div[1]/form/div[2]/div/input')
uername.send_keys('email@') # python123官网的用户名
passworld.send_keys('password') # python123的账户密码
login = web.find_element_by_xpath(
'//*[@id="links"]/div[1]/div[2]/div/div[1]/div[1]/div/div[1]/div/div[1]/div[3]/div[1]/button')
login.click()
sleep(1)
web.get('https://www.python123.io/student/courses/1521/intro')
sleep(2)
web.find_element_by_xpath('//span[text()=10]').click()
flag = True
for i in range(10):
if i == 9:
flag = False
tumu_list = web.find_elements_by_xpath(
'//*[@id="app"]/div/div[1]/div[1]/div[2]/section/div/div/div[2]/div/section/div/div/div[2]/div[2]/div/div[2]/div[2]/div[2]/div')
tumu_chuli(tumu_list[i], flag)
web.back() # 返回上一页
web.back()
web.back()
sleep(2)
web.close()
def save(failname, content):
''' 保存到文件 :param failname:文件名 :param content:内容 :return: '''
with open(failname, 'a', encoding="utf-8") as f:
f.write(content)
f.close()
def tumu_chuli(list_timu, flag):
failname = list_timu.find_element_by_tag_name('b').text[0:3]+".txt"
list_timu.click()
sleep(2)
count = 2
if flag:
web.find_element_by_xpath(
'//*[@id="group-wrapper"]/div/div[2]/div/div/div/div[2]/div/div/div[2]/div[2]/div/div[1]/div/div/div[1]').click()
xuanze = web.find_element_by_xpath(
'//*[@id="group-wrapper"]/div/div[2]/div/div/div/div[2]/div/div[1]/div/div/div[1]')
item = xuanze.find_element_by_tag_name('b').text
xuanze_list = xuanze.find_elements_by_class_name('card')
save(failname, item)
for xz in xuanze_list:
save(failname, "\n"+xz.text+"\n")
web.back()
sleep(2)
web.find_element_by_xpath(
'//*[@id="group-wrapper"]/div/div[2]/div/div/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div/div[1]').click()
for q in range(count):
bs_chuli(failname)
else:
count = 4
web.find_element_by_xpath(
'//*[@id="group-wrapper"]/div/div[2]/div/div/div/div[2]/div/div/div[2]/div[2]/div/div/div/div/div/div[1]').click()
for j in range(count):
bs_chuli(failname)
print(failname+"写入完成!")
# 编程题的处理
def bs_chuli(failname):
sleep(1)
bs_timu = web.find_element_by_class_name('mce-content-body').text
# 由于网站的代码中有特殊字符,所以用了以下的方式替换了所有特殊字符
bs_daan_my = web.find_element_by_xpath(
'/html/body/div/div/div[1]/div[1]/div[2]/section/div/div/div[2]/div/section/div/div/div[2]/div/div/div/div[2]/div/div/div/div[1]/div/div[3]/div/div/div/div[3]/div/div/div/div[2]/div/div[3]'
).text.replace('·', ' ').replace('¤', '').replace('¬', '').replace('¶', '')
web.find_element_by_xpath(
'/html/body/div/div/div[1]/div[1]/div[2]/section/div/div/div[2]/div/section/div/div/div[2]/div/div/div/div[2]/div/div/div/div[1]/div/div[3]/div/div/div/div[2]/ul/li[3]').click()
sleep(1)
bs_daan_cankao = web.find_element_by_xpath(
'/html/body/div/div/div[1]/div[1]/div[2]/section/div/div/div[2]/div/section/div/div/div[2]/div/div/div/div[2]/div/div/div/div[1]/div/div[3]/div/div/div/div[5]/div/div/div/div').text
web.find_element_by_xpath(
'//button[2]').click()
save(failname, "\n"+bs_timu+"\n【我的答案】\n"+bs_daan_my+"\n"+bs_daan_cankao)
sleep(1)
get_element()
如果觉得《python123选择题及答案_py利用selenium库 爬取 python123官网的练习题及答案》对你有帮助,请点赞、收藏,并留下你的观点哦!