用这个程序前确保能正常使用selenium模块调起浏览器,我代码使用的是谷歌浏览器,如需更改在代码里更改,代码写有注释,如不能调起浏览器百度查下怎么安装
代码能直接运行,因为这是我会了之后写的,不知道有哪些对萌新不友好的地方,欢迎指出,看到了我会抽空修改,有能力的也可以二次开发
[Python] 纯文本查看 复制代码 """
练习抓取页面标签、元素
"""
from selenium import webdriver
import selenium
#from selenium.webdriver.common.keys import Keys
from time import sleep
class element_main():
def __init__(self):
#如需使用其他浏览器,在这里修改self.diver=webdriver.你使用的浏览器
#如火狐是 :self.diver=webdriver.Firefox()
self.diver=webdriver.Chrome()
self.diver.get("https://www.baidu.com/")
def html_info(self,html):
self.diver.get(html)
def page_element_name(self,diver=None,name=None):
return self.diver.find_element_by_name(f"{name}")
def page_element_id(self,diver=None,id=None):
diver=self.diver.find_element_by_id(f"{id}")
print(diver)
return diver
def page_element_class_name(self,diver=None,classname=None):
diver=self.diver.find_element_by_class_name(f"{classname}")
print(diver)
return diver
def page_element_xpath(self,diver=None,xpath=None):
diver=self.diver.find_element_by_xpath(f"{xpath}")
print(diver)
return diver
def page_element_link_text(self,diver=None,link_text=None):
diver=self.diver.find_element_by_link_text(f"{link_text}")
print(diver)
return diver
def page_element_css_selector(self,diver=None,css_selector=None):
diver=self.diver.find_element_by_css_selector(f"{css_selector}")
print(diver)
return diver
def page_element_tag_name(self,diver=None,tag_name=None):
diver=self.diver.find_element_by_tag_name(f"{tag_name}")
print(diver)
return diver
def page_element_partial_link_text(self,diver=None,partial_link_text=None):
diver=self.diver.find_element_by_partial_link_text(f"{partial_link_text}")
print(diver)
return diver
@staticmethod
def show_menu():
sleep(2)
print('请选择如下功能:')
print('1.练习捕捉页面find_element_by_name:')
print('2.练习捕捉页面find_element_by_id:')
print('3.练习捕捉页面find_element_by_class_name:')
print('4.练习捕捉页面find_element_by_xpath:')
print('5.练习捕捉页面find_element_by_link_text:')
print('6.练习捕捉页面find_element_by_css_selector:')
print('7.练习捕捉页面find_element_by_tag_name:')
print('8.练习捕捉页面find_element_by_partial_link_text:')
print('9.建议直接在浏览器上输入网址,跳转网页,输入格式:https://www.baidu.com/')
print('10.结束练习:')
def str_input(self):
input_name=str(input("输入你的值"))
return input_name
def run_main(self):
while True:
self.show_menu()
num_menu=int(input("输入你需要练习功能的序号"))
if num_menu==1:
while True:
try:
data_name=self.page_element_name(name=self.str_input())
if not data_name==None:
data_menu=int(input("输入你下一步的操作: \n1:点击操作 \n2:输入内容"))
if data_menu==1:
data_name.click()
elif data_menu==2:
data_name.send_keys('恭喜捕捉成功')
else:
print('没有这个选项,惩罚你瞎选,重新来')
except selenium.common.exceptions.NoSuchElementException:
print("捕捉失败,重新输入")
else:
break
elif num_menu==2:
while True:
try:
data_name=self.page_element_id(id=self.str_input())
if not data_name==None:
data_menu=int(input("输入你下一步的操作: \n1:点击操作 \n2:输入内容"))
if data_menu==1:
data_name.click()
elif data_menu==2:
data_name.send_keys('恭喜捕捉成功')
else:
print('没有这个选项,惩罚你瞎选,重新来')
except selenium.common.exceptions.NoSuchElementException:
print("捕捉失败,重新输入")
else:
break
elif num_menu==3:
while True:
try:
data_name=self.page_element_class_name(classname=self.str_input())
if not data_name==None:
data_menu=int(input("输入你下一步的操作: \n1:点击操作 \n2:输入内容"))
if data_menu==1:
data_name.click()
elif data_menu==2:
data_name.send_keys('恭喜捕捉成功')
else:
print('没有这个选项,惩罚你瞎选,重新来')
except selenium.common.exceptions.NoSuchElementException:
print("捕捉失败,重新输入")
else:
break
elif num_menu==4:
while True:
try:
data_name=self.page_element_xpath(xpath=self.str_input())
if not data_name==None:
data_menu=int(input("输入你下一步的操作: \n1:点击操作 \n2:输入内容"))
if data_menu==1:
data_name.click()
elif data_menu==2:
data_name.send_keys('恭喜捕捉成功')
else:
print('没有这个选项,惩罚你瞎选,重新来')
except selenium.common.exceptions.NoSuchElementException:
print("捕捉失败,重新输入")
else:
break
elif num_menu==5:
while True:
try:
data_name=self.page_element_link_text(link_text=self.str_input())
if not data_name==None:
data_menu=int(input("输入你下一步的操作: \n1:点击操作 \n2:输入内容"))
if data_menu==1:
data_name.click()
elif data_menu==2:
data_name.send_keys('恭喜捕捉成功')
else:
print('没有这个选项,惩罚你瞎选,重新来')
except selenium.common.exceptions.NoSuchElementException:
print("捕捉失败,重新输入")
else:
break
elif num_menu==6:
while True:
try:
data_name=self.page_element_css_selector(css_selector=self.str_input())
if not data_name==None:
data_menu=int(input("输入你下一步的操作: \n1:点击操作 \n2:输入内容"))
if data_menu==1:
data_name.click()
elif data_menu==2:
data_name.send_keys('恭喜捕捉成功')
else:
print('没有这个选项,惩罚你瞎选,重新来')
except selenium.common.exceptions.NoSuchElementException:
print("捕捉失败,重新输入")
else:
break
elif num_menu==7:
while True:
try:
data_name=self.page_element_tag_name(tag_name=self.str_input())
if not data_name==None:
data_menu=int(input("输入你下一步的操作: \n1:点击操作 \n2:输入内容"))
if data_menu==1:
data_name.click()
elif data_menu==2:
data_name.send_keys('恭喜捕捉成功')
else:
print('没有这个选项,惩罚你瞎选,重新来')
except selenium.common.exceptions.NoSuchElementException:
print("捕捉失败,重新输入")
else:
break
elif num_menu==8:
while True:
try:
data_name=self.page_element_partial_link_text(partial_link_text=self.str_input())
if not data_name==None:
data_menu=int(input("输入你下一步的操作: \n1:点击操作 \n2:输入内容"))
if data_menu==1:
data_name.click()
elif data_menu==2:
data_name.send_keys('恭喜捕捉成功')
else:
print('没有这个选项,惩罚你瞎选,重新来')
except selenium.common.exceptions.NoSuchElementException:
print("捕捉失败,重新输入")
else:
break
elif num_menu==9:
html_web=str(input("建议直接在浏览器上输入网址\n输入你要进入的网址"))
self.html_info(html_web)
elif num_menu==10:
self.diver.quit()
break
if __name__=="__main__":
main_run=element_main()
main_run.run_main()
|