c 本地html解析,使用selenium解析本地HTML文件方法

import requests

import time

"""

经验总结:

1.使用selenium解析本地HTML文件方法,

browser.get("file://C:/Users/23242/Desktop/HTML/People List _ USDA ARS.html")

file://很重要不能少;

2.(.text)获取不到时,换成.get_attribute("textContent")

3.(.replace( ,)).replace( ,))替换空格和换行

4.有时候界面的弹出框,并不是一个子ifram 其实代码就在界面上,注意观察

"""

from selenium import webdriver

browser=webdriver.Chrome()

browser.get("file://C:/Users/Desktop/HTML/People List _ USDA ARS.html")

# print(browser.page_source)

# time.sleep(3)

#

button=browser.find_element_by_xpath(//*[@id="prefix-overlay-header"]/button)

button.click()

# 打开所有界面

# button=browser.find_element_by_id(expandAll)

# button.click()

title=browser.find_elements_by_xpath(//ul[@class="usa-accordion-bordered"]/li/button)

print(len(title))

for i in range(0,len(title)):

# print(title[i].text)#打印标题 列表是从0开始 xpath是从1开始

path=(//ul[@class="usa-accordion-bordered"]/li[{0}]/div/ul/li/a).format(i+1)

# print(path)

name=browser.find_elements_by_xpath(path)

# print(len(name))

for j in range(0,len(name)):

name_little=((name[j].get_attribute("textContent")).replace( ,)).replace( ,)

# print(name_little)

# print( )#打印名字

path_one=(//ul[@class="usa-accordion-bordered"]/li[{0}]/div/ul/li[{1}]).format(i+1,j+1)

# print(path_one)

name_content=browser.find_elements_by_xpath(path_one)

for k in range(len(name_content)): #打印名字的内容

cont=((name_content[k].get_attribute("textContent")).replace( ,)).replace( ,)

print(cont)

import requests import time """ 经验总结: 1.使用selenium解析本地HTML文件方法, browser.get("file://C:/Users/23242/Desktop/HTML/People List _ USDA ARS.html") file://很重要不能少; 2.(.text)获取不到时,换成.get_attribute("textContent") 3.(.replace( ,)).replace( ,))替换空格和换行 4.有时候界面的弹出框,并不是一个子ifram 其实代码就在界面上,注意观察 """ from selenium import webdriver browser=webdriver.Chrome() browser.get("file://C:/Users/Desktop/HTML/People List _ USDA ARS.html") # print(browser.page_source) # time.sleep(3) # button=browser.find_element_by_xpath(//*[@id="prefix-overlay-header"]/button) button.click() # 打开所有界面 # button=browser.find_element_by_id(expandAll) # button.click() title=browser.find_elements_by_xpath(//ul[@class="usa-accordion-bordered"]/li/button) print(len(title)) for i in range(0,len(title)): # print(title[i].text)#打印标题 列表是从0开始 xpath是从1开始 path=(//ul[@class="usa-accordion-bordered"]/li[{0}]/div/ul/li/a).format(i+1) # print(path) name=browser.find_elements_by_xpath(path) # print(len(name)) for j in range(0,len(name)): name_little=((name[j].get_attribute("textContent")).replace( ,)).replace( ,) # print(name_little) # print( )#打印名字 path_one=(//ul[@class="usa-accordion-bordered"]/li[{0}]/div/ul/li[{1}]).format(i+1,j+1) # print(path_one) name_content=browser.find_elements_by_xpath(path_one) for k in range(len(name_content)): #打印名字的内容 cont=((name_content[k].get_attribute("textContent")).replace( ,)).replace( ,) print(cont)
经验分享 程序员 微信小程序 职场和发展