吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2429|回复: 11
收起左侧

[Python 原创] python爬取建安练习题库,包含答案

[复制链接]
16713720339 发表于 2022-12-11 14:27
[Python] 纯文本查看 复制代码
#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
for uuu in range(1,11):
    if uuu != 10:
        uurl = "0"+ str(uuu)

    url = 'http://www.henanjianan.com/tiku/dianhan/'+uurl+'.html'

    driver = webdriver.Edge()

    driver.get(url)

    time.sleep(5)

    driver.find_element_by_xpath("/html/body/form/div[5]/input").click()#点击按钮
    time.sleep(3)
    alertObject = driver.switch_to.alert
    print(alertObject.text)  # 打印提示信息
    time.sleep(1)
    alertObject.accept()  # 点击确定按钮

    for n in range(30):
        r=1000
        panduanti = "XXLD"+ str(n)
        daan = "Tip"+str(r)
        #单选题目
        aswes = driver.find_element(By.XPATH,'//*[@id="'+str(panduanti)+'"]/div[1]')

        print(aswes.text)
        with open("D:/test2.txt","a+") as f:
            f.write(aswes.text)  # 自带文件关闭功能,不需要再写f.close()

        #答案
        aswes = driver.find_element(By.XPATH,'//*[@id="'+daan+'"]/div[2]')
        print(aswes.text)
        with open("D:/test2.txt","a+") as f:
            f.write(aswes.text)  # 自带文件关闭功能,不需要再写f.close()
            f.write('\n')
        r=r+1

        #选项
        aswes = driver.find_element(By.XPATH,'//*[@id="'+str(panduanti)+'"]/div[2]')

        print(aswes.text)
        with open("D:/test2.txt","a+") as f:
            f.write(aswes.text)  # 自带文件关闭功能,不需要再写f.close()
            f.write('\n')

        aswes = driver.find_element(By.XPATH,'//*[@id="'+str(panduanti)+'"]/div[3]')

        print(aswes.text)
        with open("D:/test2.txt","a+") as f:
            f.write(aswes.text)  # 自带文件关闭功能,不需要再写f.close()
            f.write('\n')

        aswes = driver.find_element(By.XPATH,'//*[@id="'+str(panduanti)+'"]/div[4]')

        print(aswes.text)
        with open("D:/test2.txt","a+") as f:
            f.write(aswes.text)  # 自带文件关闭功能,不需要再写f.close()
            f.write('\n')

    #判断题目
    for n in range(70):
        r=1030
        panduanti = "PDT"+str(n)
        daan = "Tip"+str(r)
        aswes = driver.find_element(By.XPATH,'//*[@id="'+panduanti+'"]/div[1]')

        print(aswes.text)
        with open("D:/test2.txt","a+") as f:
            f.write(aswes.text)  # 自带文件关闭功能,不需要再写f.close()

        aswes = driver.find_element(By.XPATH,'//*[@id="'+daan+'"]/div[2]')
        print(aswes.text)
        with open("D:/test2.txt","a+") as f:
            f.write(aswes.text)  # 自带文件关闭功能,不需要再写f.close()
            f.write('\n')
        r=r+1

    driver.close()





会输出到D盘根目录下的test.txt文本中

免费评分

参与人数 3吾爱币 +3 热心值 +2 收起 理由
notifier + 1 + 1 我很赞同!
blackid + 1 + 1 Traceback (most recent call last): File "D:\test.py", line
ailmdg + 1 已经处理,感谢您对吾爱破解论坛的支持!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

tianyagd 发表于 2022-12-11 19:13
本帖最后由 tianyagd 于 2022-12-11 19:17 编辑
嬉皮笑脸 发表于 2022-12-11 15:52
用不了,报
AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'

你没有根据你的浏览器版本来安装对应插件  这个是edge的,另外修改为
driver.find_element_by_xpath("/html/body/form/div[5]/input").click()#点击按钮

driver.find_element(By.XPATH,"/html/body/form/div[5]/input").click()#点击按钮
语法修改了
wangsking 发表于 2022-12-12 09:08
blackid 发表于 2022-12-11 22:21
Traceback (most recent call last):
  File "D:\test.py", line 3, in
    from bs4 import BeautifulS ...

基础的模块没有装,要先安装Beautifulsoup,pip命令安装
嬉皮笑脸 发表于 2022-12-11 15:52
用不了,报
AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'
blackid 发表于 2022-12-11 19:29
Traceback (most recent call last):
  File "D:\test.py", line 2, in <module>
    from selenium import webdriver
ModuleNotFoundError: No module named 'selenium'
tianyagd 发表于 2022-12-11 19:57
blackid 发表于 2022-12-11 19:29
Traceback (most recent call last):
  File "D:\test.py", line 2, in
    from selenium import webdr ...

你都没有装selenium模块,另外还需要webdriver
繁华什锦 发表于 2022-12-11 21:13
感谢分享&#128591;
akillking 发表于 2022-12-11 21:20
支持原创,感谢分享
嬉皮笑脸 发表于 2022-12-11 21:57
tianyagd 发表于 2022-12-11 19:13
你没有根据你的浏览器版本来安装对应插件  这个是edge的,另外修改为
driver.find_element_by_xpath("/h ...

nice,改成find_element(By.XPATH   可以了。
blackid 发表于 2022-12-11 22:21
Traceback (most recent call last):
  File "D:\test.py", line 3, in <module>
    from bs4 import BeautifulSoup
ModuleNotFoundError: No module named 'bs4'
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 01:38

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表