我要做的是为.csv
创建搜索查询和解析数据的循环。但是代码在此行driver.find_element_by_partial_link_text(query).click()
上出错,而Selenium停止工作。无法弄清楚发生了什么。
from selenium import webdriver
from bs4 import BeautifulSoup
import html5lib
import lxml.html
import csv
import time
driver = webdriver.Chrome()
driver.get("url"
with open('your_csv.csv','r') as csvfile:
with open('log.csv','wb') as csv_out:
writer=csv.writer(csv_out)
spamreader = csv.reader(csvfile)
for query in spamreader:
username = driver.find_element_by_name("p_name")
username.send_keys(query)
driver.find_element_by_xpath("html/body/form/table[6]/tbody/tr/td[2]/input").click() #this will take you on new link
time.sleep(4)
driver.find_element_by_partial_link_text(query).click()
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
words = soup.find_all("td")
ralph = soup.get_text().encode('utf-8')
a = list()
for jav in words:
a.append((jav.text).encode('utf-8'))
with open('data.csv', 'w') as csvfile:
spamwriter = csv.writer(csvfile,delimiter=',')
spamwriter.writerow(a)
这是错误:
Traceback (most recent call last):
File "ninja.py", line 21, in <module>
driver.find_element_by_partial_link_text(query).click()
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 361, in find_element_by_partial_link_text
return self.find_element(by=By.PARTIAL_LINK_TEXT, value=link_text)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 791, in find_element
'value': value})['value']
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
self.error_handler.check_response(response)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: 'value' must be a string
(Session info: chrome=58.0.3029.110)
(Driver info: chromedriver=2.29.461585 (0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b),platform=Mac OS X 10.12.5 x86_64)
答案 0 :(得分:1)
我发现csv.reader()返回每一行,作为字符串列表。方括号。例如,如果CSV文件中的行包含。 “贾维斯”。它将返回[“jarvis”],这完全没有语法。这就是我所做的。只是条形方括号......
with open('your_csv.csv','r') as csvfile:
with open('log.csv','wb') as csv_out:
writer=csv.writer(csv_out)
spamreader = csv.reader(csvfile)
for query in spamreader:
username = driver.find_element_by_name("p_name")
username.send_keys(str(query).strip('[').strip(']')
...
答案 1 :(得分:0)