字符串转化为列表
试了下把打印结果放到p_list[] 作为列表显示, 结果报错, AttributeError: 'list' object has no attribute 'text'。麻烦看下要怎么修改?
from selenium import webdriver
wd = webdriver.Chrome(r'C:\Users\sesa395799\Downloads\chromedriver_win32\chromedriver.exe')
wd.get('https://www.digikey.cn/zh/products/category/resistors/2')
mainWindow = wd.current_window_handle
element = wd.find_element_by_css_selector("#header > div.header__top > div.header__search > div > div.searchbox-inner > div.searchbox-inner-searchtext > input")
list = ['12FR010E-N\n','MP930-15.0F-ND\n']
p_list = []
for i in :
element.send_keys(list)
p_list = wd.find_elements_by_css_selector('#product-attributes > tbody > tr > td > div')
print(p_list)
wd.switch_to.window(mainWindow)
element = wd.find_element_by_css_selector("#header > div.header__top > div.header__search > div > div.searchbox-inner > div.searchbox-inner-searchtext > input")
wd.quit() 没人回复, 顶一下{:5_109:} from selenium import webdriver
from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support import expected_conditions as expected
from selenium.webdriver.support.wait import WebDriverWait
def main():
data = dict()
inpt = ['12FR010E-ND', 'CF1/4CT52R101J', 'MR40MB12.70R001JT', 'RNF18FTD1K00']
browser = webdriver.Firefox()
ip = "#header > div.header__top > div.header__search > div > div.searchbox-inner > div.searchbox-inner-searchtext > input"
rs = "#product-attributes > tbody > tr > td > div"
bt = "#header-search-button"
browser.get('https://www.digikey.cn/zh/products/category/resistors/2')
wait = WebDriverWait(browser, 10)
wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, ip)))
for s in inpt:
t_data = []
browser.switch_to.window(browser.current_window_handle)
browser.find_element_by_css_selector(ip).send_keys(s)
browser.find_element_by_css_selector(bt).click()
try:
wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, rs)))
result = browser.find_elements_by_css_selector(rs)
for r in result:
if r.text:
t_data.append(r.text)
data = t_data
except Exception as e:
print(e)
print(data)
if __name__ == "__main__":
main()
谢谢!运行成功了。
但是结果是 inpt 里所有的元素都在一个dict里,怎么调整可以把inpt 里每个元素的结果单独打印?
inpt = ['12FR010E-ND', 'CF1/4CT52R101J', 'MR40MB12.70R001JT', 'RNF18FTD1K00'] suchocolate 发表于 2021-6-1 17:32
谢谢!运行成功了。
但是结果是 inpt 里所有的元素都在一个dict里,怎么调整可以把inpt 里每个元素的结果单独打印?
inpt = ['12FR010E-ND', 'CF1/4CT52R101J', 'MR40MB12.70R001JT', 'RNF18FTD1K00'] 我试着 print(t_data),不 print(data), 但是这样 inpt 里的元素就打印不出来了。。 本帖最后由 suchocolate 于 2021-6-2 00:09 编辑
grey_lemon 发表于 2021-6-1 22:39
我试着 print(t_data),不 print(data), 但是这样 inpt 里的元素就打印不出来了。。
from selenium import webdriver
from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support import expected_conditions as expected
from selenium.webdriver.support.wait import WebDriverWait
def main():
inpt = ['12FR010E-ND', 'CF1/4CT52R101J', 'MR40MB12.70R001JT', 'RNF18FTD1K00']
browser = webdriver.Firefox()
ip = "#header > div.header__top > div.header__search > div > div.searchbox-inner > div.searchbox-inner-searchtext > input"
rs = "#product-attributes > tbody > tr > td > div"
bt = "#header-search-button"
browser.get('https://www.digikey.cn/zh/products/category/resistors/2')
wait = WebDriverWait(browser, 10)
wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, ip)))
for s in inpt:
t_data = []
browser.switch_to.window(browser.current_window_handle)
browser.find_element_by_css_selector(ip).send_keys(s)
browser.find_element_by_css_selector(bt).click()
try:
wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, rs)))
result = browser.find_elements_by_css_selector(rs)
for r in result:
if r.text:
t_data.append(r.text)
print(s, t_data)
except Exception as e:
print(e)
if __name__ == "__main__":
main()
只要拿到了数据,怎样print,怎样转换都看你自己的需求。
我觉得有空你多看看字符串,字典什么的基本操作,这些基本功比爬虫简单多了。 suchocolate 发表于 2021-6-2 00:06
只要拿到了数据,怎样print,怎样转换都看你自己的需求。
我觉得有空你多看看字符串,字典什么的基 ...
好的,谢谢指导!
页:
[1]