嗨,我一直在使用python脚本来收集结果列表,并将这些结果写入csv文件。
我的代码可以工作,但它将结果写入一个单元格(用"~"连接(。我想把每个结果都写进一行。
我没有太多经验,我确实在力所能及的范围内进行了搜索。如果你觉得有什么东西在躲避我,你能告诉我我应该搜索什么吗?
这是我当前的代码:
person_result_names = WebDriverWait(browser, 60, ignored_exceptions=ignored_exceptions).until(EC.visibility_of_element_located((By.ID, 'person_results'))).find_elements_by_css_selector("#person_info > div > div > div.name.variation")
wait_time(1)
all_persons = [ person.text.replace(',', '').replace('n', ' ') for person in person_result_names ]
print(f"[**] Found {len(all_persons)} people")
person = '~'.join(all_persons)
print(person)
report_links = browser.find_elements_by_css_selector('#person_results > div > a')
all_urls = [ url.get_attribute('href') for url in report_links ]
print(f"[**] Found {len(all_urls)} report urls")
url = '~'.join(all_urls)
print(url)
time.sleep(5)
with open('3_results-output.csv', 'a') as fp:
print(f"{row_id},{person},{url}", file=fp)
不要担心最后一行中的row_id
变量。它是从另一个来源提取的。提前感谢所有有用的解决方案。
这是我当前的输出:
ID
表示每个搜索查询,因此ID 0
对于Names和Url都有3个结果。ID 1
每个有4个结果
ID | Names | Urls
0 | John Doe~Joe Doe~Jay Doe | http://www.link.com/1~http://www.link.com/2~http://www.link.com/3
1 | Jane Doe~Janet Doe~Jill Doe~Julia Doe | http://www.link.com/4~http://www.link.com/5~http://www.link.com/6~http://www.link.com/7
这就是我希望输出的样子:
ID | Names | Urls
0 | John Doe | http://www.link.com/1
0 | Joe Doe | http://www.link.com/2
0 | Jay Doe | http://www.link.com/3
1 | Jane Doe | http://www.link.com/4
1 | Janet Doe | http://www.link.com/5
1 | Jill Doe | http://www.link.com/6
1 | Julia Doe | http://www.link.com/7
更新
这是最终为我工作的代码。感谢@Tim Post为我指明了正确的方向。我只需要写的方法包括所有的结果。下面的代码对我有效:
person_result_names = WebDriverWait(browser, 60, ignored_exceptions=ignored_exceptions).until(EC.visibility_of_element_located((By.ID, 'person_results'))).find_elements_by_css_selector("#person_info > div > div > div.name.variation")
wait_time(1)
print("tScraping Peoplen")
all_persons = [ person.text.replace(',', '').replace('n', ' ') for person in person_result_names ]
print(f"[**] Found {len(all_persons)} people")
wait_time(1)
report_links = browser.find_elements_by_css_selector('#person_results > div > a')
all_urls = [ url.get_attribute('href') for url in report_links ]
print(f"[**] Found {len(all_urls)} report urls")
with open('3_results-output.csv', 'a') as fp:
for person, url in zip(all_persons, all_urls):
print(f"{row_id},{person},{url}", file=fp)
尝试以下类型的方法。它利用Python的CSV库来帮助将行写入文件:
import csv
person_result_names = WebDriverWait(browser, 60, ignored_exceptions=ignored_exceptions).until(EC.visibility_of_element_located((By.ID, 'person_results'))).find_elements_by_css_selector("#person_info > div > div > div.name.variation")
wait_time(1)
all_persons = [person.text.replace(',', '').replace('n', ' ') for person in person_result_names]
print(f"[**] Found {len(all_persons)} people")
report_links = browser.find_elements_by_css_selector('#person_results > div > a')
all_urls = [ url.get_attribute('href') for url in report_links ]
print(f"[**] Found {len(all_urls)} report urls")
with open('3_results-output.csv', 'w', newline='') as f_output:
csv_output = csv.writer(f_output)
csv_output.writerow(["ID", "Names", "Urls"]) # Write the header
row_id = 0
for person, url in zip(all_persons, all_urls):
csv_output.writerow([row_id, person, url])
显然,您需要在某个地方添加另一个循环来处理多个row_id
,但这至少说明了如何正确地写入文件。它假定all_persons
和all_urls
具有相等的长度。