把下面代码读取的内容写入csv文件import time from selenium import webdriver driver = webdriver.Chrome() driver.implicitly_wait(10) driver.get("https://www.shanghairanking.cn/institution") name = driver.find_elements_by_xpath('//*[@id="__layout"]/div/div[2]/div[2]/div[1]/div/div/div[2]/div[1]/span') address = driver.find_elements_by_xpath('//*[@id="__layout"]/div/div[2]/div[2]/div[1]/div/div/div[2]/div[3]/span[1]') for name_1, address_name in zip(name, address): print(name_1.text, address_name.text) driver.quit()
时间: 2024-01-22 16:02:03 浏览: 143
selenium_webdriver学习
可以使用csv模块将读取的内容写入csv文件中。完整的代码如下:
```python
import time
import csv
from selenium import webdriver
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("https://www.shanghairanking.cn/institution")
results = driver.find_elements_by_css_selector('.university-name a')
addresses = driver.find_elements_by_css_selector('.univ-address')
rows = []
for name, address in zip(results, addresses):
rows.append([name.text, address.text])
with open('universities.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(['Name', 'Address'])
writer.writerows(rows)
driver.quit()
```
这样就可以将大学名称和地址写入到名为"universities.csv"的csv文件中了。
阅读全文