在搜索栏里面一次性输出了data的值,我需要是一个一个值进行操作
import re
import time
import requests
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
import csv
with open('a.csv', 'r') as file:
reader = csv.reader(file)
data = list(reader)
driver = webdriver.Firefox()
driver.get('https://www.tianyancha.com/')
driver.find_element(By.CSS_SELECTOR,'.treasure_nav-link__7ErdH').click()
sleep(3)
for row in data:
driver.find_element(By.CSS_SELECTOR,"input._7c380").send_keys(row)
driver.find_element(By.CSS_SELECTOR,".index_home-suggest-button__GuWyT > span").click()
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.0',
'Referer': 'https://www.tianyancha.com/',
'Cookie': 'jsid=SEO-BAIDU-ALL-SY-000001; TYCID=83b03e70d99a11edaff321a5cbfd05e4; '
'sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22285668898%22%2C%22first_id%22%3A'
'%22187783a76c9a-0ca1d139855f728-d575429-2073600-187783a76ca66c%22%2C%22props%22%3A%7B%22'
'%24latest_traffic_source_type%22%3A%22%E8%87%AA%E7%84%B6%E6%90%9C%E7%B4%A2%E6%B5%81%E9%87%8F%22'
'%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22'
'%24latest_referrer%22%3A%22https%3A%2F%2Fwww.baidu.com%2Flink%22%7D%2C%22identities%22%3A%2'
'…4576; tyc-user-info-save-time=1683278954496; bannerFlag=true; HWWAFSESID=192202628114f7efe79; '
'HWWAFSESTIME=1683276657901; csrfToken=ktIfQ-X_48aoMG7hj6jkWsbh; '
'Hm_lpvt_e92c8d65d92d534b0fc290df538b4758=1683276673; '
'tyc-user-info=%7B%22state%22%3A%220%22%2C%22vipManager%22%3A%220%22%2C%22mobile%22%3A'
'%2218273703032%22%7D; '
'auth_token=eyJhbGciOiJIUzUxMiJ9'
'.eyJzdWIiOiIxODI3MzcwMzAzMiIsImlhdCI6MTY4MzI3ODk2NywiZXhwIjoxNjg1ODcwOTY3fQ'
'.GcKezvllS_vl1UzhXXZK_4JgqYiFklGkmsZIRv0dg2OZee1oo2H-BSe0LcM16Gju1wfKBoQUMJUNQN4HCvZnWA '
}
try:
company= driver.find_element(By.CSS_SELECTOR,'.index_alink__zcia5 > span:nth-child(1)').text
name = driver.find_element(By.CSS_SELECTOR,'div.index_info-col__UVcZb:nth-child(1) > a:nth-child(1)').text
tel = driver.find_element(By.XPATH,'/html/body/div[1]/div[2]/div/div[2]/section/main/div[2]/div[2]/div/div/div[2]/div[2]/div[4]/div[1]/span[2]/span').text
address= driver.find_element(By.CSS_SELECTOR,'div.index_contact-row__iYUn6:nth-child(5) > div:nth-child(1) > span:nth-child(2)').text
print(company, name, tel, address)
except NoSuchElementException:
pass
#代码无报错,问题是该代码一次性把data里面的数据全部输入了,我需要一个一个去执行