- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
通过阅读、视频、SO 和社区的帮助,我能够从 Tessco.com 中抓取数据。使用 Selenium 和 Python。
该网站需要 UN 和 PW。我已将其包含在下面的代码中,这是非必要的凭据,专门用于提问。
我的最终目标是循环浏览 Excel 零件号列表,并搜索包括价格在内的一组参数。在引入循环列表之前,我希望将所需信息与抓取的信息分开。
我不确定如何过滤此信息。
代码如下:
import time
#Need Selenium for interacting with web elements
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
#Need numpy/pandas to interact with large datasets
import numpy as np
import pandas as pd
chrome_path = r"C:\Users\James\Documents\Python Scripts\jupyterNoteBooks\ScrapingData\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://www.tessco.com/login")
userName = "FirstName.SurName321123@gmail.com"
password = "PasswordForThis123"
#Set a wait, for elements to load into the DOM
wait10 = WebDriverWait(driver, 10)
wait20 = WebDriverWait(driver, 20)
wait30 = WebDriverWait(driver, 30)
elem = wait10.until(EC.element_to_be_clickable((By.ID, "userID")))
elem.send_keys(userName)
elem = wait10.until(EC.element_to_be_clickable((By.ID, "password")))
elem.send_keys(password)
#Press the login button
driver.find_element_by_xpath("/html/body/account-login/div/div[1]/form/div[6]/div/button").click()
#Expand the search bar
searchIcon = wait10.until(EC.element_to_be_clickable((By.XPATH, "/html/body/header/div[2]/div/div/ul/li[2]/i")))
searchIcon.click()
searchBar = wait10.until(EC.element_to_be_clickable((By.XPATH, '/html/body/header/div[3]/input')))
searchBar.click()
#load in manufacture part number from a collection of components, via an Excel file
#Enter information into the search bar
searchBar.send_keys("HL4RPV-50" + '\n')
# wait for the products information to be loaded
products = wait30.until(EC.presence_of_all_elements_located((By.XPATH,"//div[@class='CoveoResult']")))
# create a dictionary to store product and price
productInfo = {}
# iterate through all products in the search result and add details to dictionary
for product in products:
# get product info such as OEM, Description and Part Number
productDescr = product.find_element_by_xpath(".//a[@class='productName CoveoResultLink hidden-xs']").text
mfgPart = product.find_element_by_xpath(".//ul[@class='unlisted info']").text.split('\n')[3]
mfgName = product.find_element_by_tag_name("img").get_attribute("alt")
# get price
price = product.find_element_by_xpath(".//div[@class='price']").text.split('\n')[1]
# add details to dictionary
productInfo[mfgPart, mfgName, productDescr] = price
# print products information
print(productInfo)
输出为
{('MFG PART #: HL4RPV-50', 'CommScope', '1/2" Plenum Air Cable, Off White'): '$1.89', ('MFG PART #: HL4RPV-50B', 'CommScope', '1/2" Plenum Air Cable, Blue'): '$1.89', ('MFG PART #: L4HM-D', 'CommScope', '4.3-10 Male for 1/2" AL4RPV-50,LDF4-50A,HL4RPV-50'): '$19.94', ('MFG PART #: L4HR-D', 'CommScope', '4.3-10M RA for 1/2" AL4RPV-50, LDF4-50A, HL4RPV-50'): '$39.26', ('MFG PART #: UPL-4MT-12', 'JMA Wireless', '4.3-10 Male Connector for 1/2” Plenum Cables'): '$32.99', ('MFG PART #: UPL-4F-12', 'JMA Wireless', '4.3-10 Female Connector for 1/2" Plenum'): '$33.33', ('MFG PART #: UPL-4RT-12', 'JMA Wireless', '4.3-10 R/A Male Connector for 1/2" Plenum'): '$42.82', ('MFG PART #: L4HF-D', 'CommScope', '4.3-10 Female for 1/2 in AL4RPV-50, LDF4-50A'): '$20.30'}
我只想要自动搜索中引用的内容,因此对于这个示例,我将寻找
(“制造零件编号:HL4RPV-50”、“康普”、“1/2”静压空气电缆,灰白色”):“1.89 美元”
最终,我计划用项目列表替换 HL4RPV-50 标签,但现在,我认为我应该过滤所需的内容。
我怀疑逻辑是否正确,但我尝试打印符合搜索要求的任何部分的产品信息,如下所示。
for item in mfgPart:
if mfgPart == "HL4RPV-50":
print(productInfo)
但是上面的代码只是像以前一样打印了所有输出。
然后我尝试导入 itertools
并运行以下命令:
print(dict(itertools.islice(productInfo.items(), 1)))
实际上返回了我想要的订单项,但不能保证第一个返回的项目就是我正在寻找的项目。如果我可以根据给定的部件号过滤出精确的搜索,那就最好了。
有没有办法可以根据输入过滤结果?
非常感谢任何提示。
最佳答案
其他答案似乎检查零件编号是否在制造零件字符串中,但我看到某些项目可能包含相同的零件编号,例如 HL4RPV-50
和HL4RPV-50B
。如果您想隔离零件号,以便可以准确地知道您正在查看哪个零件,我建议您迭代字典,并在冒号处拆分制造零件字符串以获取 ID。您还可以抓取该项目的其他部分以更清晰地打印信息,如下例所示。
for (mfg_part, comm_scope, name), price in productInfo.items():
mfg_id = mfg_part.split(': ')[1]
if mfg_id == 'HL4RPV-50':
print('Part #:', mfg_id)
print('Company:', comm_scope)
print('Name:', name)
print('Price:', price)
关于Python、 Selenium : Isolate Item From Returned List,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56940686/
来自文档: Isolation.DEFAULT Use the default isolation level of the underlying datastore. 对于其他隔离级别,我们可以在
@Transactional(isolation = Isolation.SERIALIZABLE) 我在我的 spring 项目中的几个方法上有这个注释。如果由于“序列化访问问题”而出现异常,如果我
到目前为止我发现的“隔离”的解释。 实验#1:http://izs.me/v8-docs/classv8_1_1Isolate.html “Isolate 表示 V8 引擎的一个孤立实例。V8 分离株
我是单元测试和 Typemock 的新手。我想选择 typemock,因为我有很多遗留代码和很多依赖项。我知道 Isolator Basic 是免费版本,但他们也提供了 Isolator Comple
定义指令时,通常为它们提供一个独立的作用域,并且原型(prototype)上不会从任何父作用域继承。我一直很困惑这个术语应该是“隔离范围”还是“隔离范围”。 似乎大多数 AngularJS 官方文档都
$ pip install --no-build-isolation --editable . Usage: pip install [options] [package-index-opt
我有一个使用 mssql 的航类预订程序 ,对于预订航类,我想确定我应该使用隔离级别还是锁? (这是一个示例代码,我的问题是这种情况的隔离级别不做保留) 我的数据库有一个库存表,如: Inventor
我一直在玩 Dart Isolates并且在使用 isolate.pause(); 时遇到了问题功能。 import 'dart:io'; import 'dart:isolate'; main(){
隔离示例 flutter !不起作用。新的隔离不会运行。但是如果我删除入口点函数的 async。它工作正常。那么,如何在 flutter 中的新隔离中进行等待操作。 最佳答案 可能存在 Dart 2
当应用程序被杀死时,使用 flutter isolate 包的 Flutter isolates 不会显示通知。 尝试将 isolate 包与 dart:isolate 一起使用,但不起作用。 imp
据我了解,Dart 隔离就像 Akka Actor 。 然而,我想不通的是,如果 dart:isolate用途与 Akka 类似做。两者之间有根本区别吗? 是 dart:isolate一个 Actor
正如你们中的一些人所知道的,在 .net 中模拟静态方法通常是不可能的。 通过模拟,我的意思是用另一个具有相同签名的方法替换类中的方法,通常用于测试目的。 用于模拟方法的两种主要方法是将其声明为虚拟方
我有一个生成散列密码的应用程序,生成它需要时间。我认为为了提高性能,我会让散列密码生成器在一个单独的核心中工作。我的计算机支持 3 个核心处理器,我认为使用 dart:isolate 来计算其他处理器
我正在尝试在Dart Web应用程序中使用隔离,但是我似乎无法使错误回调参数起作用。 我有一个非常基本的代码正在Dartium中运行。 import "dart:isolate"; void main
我正在试验 Dart 并使用新的 streamSpawnFunction 创建一个新的隔离。 我在 Dartium 中运行我的代码,但我注意到如果在隔离中发生某种不可恢复的错误,我不会在控制台上收到任
我的问题的背景:我正在开发一个 Flutter 应用程序,它每隔几个小时执行一次重复的后台事件,并在有任何更新时通知用户。 目前,我已经创建了一个能够执行后台事件的隔离功能(但目前尚未安排),并且我正
我需要在 V8 中有数千个隔离物。但是当我有一些空闲 RAM 时,我遇到了奇怪的内存问题,但是 v8 抛出了 OOM 错误消息。 例如, 在这种特殊情况下,您可以看到当 V8 抛出此错误时我们仍然有一
我有大约 10k 个长时间运行的任务需要按顺序计算。为此,我决定使用 Isolates。问题是我应该每次都为每个单独的任务创建 spawn Isolate 还是应该为所有任务的执行只创建一个 Isol
Moles是微软创建的隔离框架。 Moles 的一个很酷的特性是它可以“模拟”静态/非虚拟方法和密封类(这对于像 Moq 这样的框架是不可能的)。下面是 Moles 可以做什么的快速演示: Asser
谁能解释一下垃圾收集的孤立岛的概念吗? 最佳答案 对象 A 引用对象 B。对象 B 引用对象 A。对象 A 和对象 B 都没有被任何其他对象引用。那是一座孤岛。 基本上,隔离岛是一组相互引用但不被应用
我是一名优秀的程序员,十分优秀!