代码拉取完成,页面将自动刷新
import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from utils.xpath_util import escape_string_for_xpath
# 当前文件所在的文件夹路径
current_path = os.getcwd()
# chrome浏览器路径
chrome_location = os.path.join(current_path, 'chrome-win')
# chrome.exe完整路径
browser_location = os.path.join(chrome_location, 'chrome.exe')
# chromedriver.exe的完整路径
driver_location = os.path.join(chrome_location, 'chromedriver.exe')
# 创建一个Servic对象,传入ChromeDriver的路径
service = Service(driver_location)
# 创建Chrome选项
options = Options()
# options.add_argument("--headless") # 无头模式(无界面)
# 禁用图片
# options.add_argument('blink-settings=imagesEnabled=false')
# option设置,传入Chrome浏览器的路径(chrome.exe完整路径)
options.binary_location = browser_location
# 不显示 Chrom正受到自动测试软件控制
options.add_experimental_option("excludeSwitches", ['enable-automation'])
# ip_port = '111.222.197.66:3328' # 这里是你使用的代理ip和端口
# options.add_argument(f"--proxy-server={ip_port}") # options.add_argument("--proxy-server=111.222.197.66:3328")
# 关闭webrtc
preferences = {
"webrtc.ip_handling_policy": "disable_non_proxied_udp",
"webrtc.multiple_routes_enabled": False,
"webrtc.nonproxied_udp_enabled": False
}
options.add_experimental_option("prefs", preferences)
options.add_argument('--start-maximized')
driver = webdriver.Chrome(service=service, options=options)
# driver.set_page_load_timeout(15) # 设置页面加载超时时间为15秒
# driver.set_script_timeout(15) # 设置js加载超时时间为15秒
# url = "https://httpbin.org/ip"
url = "http://httpbin.org/ip"
try:
# url = "https://www.gldzb.com/#/home?redirect=%2FprojectCost%2Findex&gid=7270474548041416941"
url="https://ec.chng.com.cn/ecmall/announcement/announcementDetail.do?announcementId=12726847"
driver.get(url)
title = '我的标题有"双引号"'
driver.save_screenshot('1.png')
# title = "我的标题有'单引号'"
# title = """我的标题有'单引号'和"双引号""" #这里使用了concat方法 用于将多个字符串连接在一起
title = escape_string_for_xpath(title)
title_xpath = f'//*[contains(.,{title})]'
driver.find_elements(By.XPATH, title_xpath)
print(driver.page_source)
except Exception as e:
print(e)
finally:
driver.quit()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。