python爬取建安练习题库,包含答案
#coding=utf-8from 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/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')
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')
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')
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')
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')
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')
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')
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文本中 本帖最后由 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/input").click()#点击按钮
为
driver.find_element(By.XPATH,"/html/body/form/div/input").click()#点击按钮
语法修改了 blackid 发表于 2022-12-11 22:21
Traceback (most recent call last):
File "D:\test.py", line 3, in
from bs4 import BeautifulS ...
基础的模块没有装,要先安装Beautifulsoup,pip命令安装 用不了,报
AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'
Traceback (most recent call last):
File "D:\test.py", line 2, in <module>
from selenium import webdriver
ModuleNotFoundError: No module named 'selenium' blackid 发表于 2022-12-11 19:29
Traceback (most recent call last):
File "D:\test.py", line 2, in
from selenium import webdr ...
你都没有装selenium模块,另外还需要webdriver 感谢分享🙏 支持原创,感谢分享 tianyagd 发表于 2022-12-11 19:13
你没有根据你的浏览器版本来安装对应插件这个是edge的,另外修改为
driver.find_element_by_xpath("/h ...
nice,改成find_element(By.XPATH 可以了。 Traceback (most recent call last):
File "D:\test.py", line 3, in <module>
from bs4 import BeautifulSoup
ModuleNotFoundError: No module named 'bs4'
页:
[1]
2