python csv教程_python基础教程利用python 读写csv文件

import csv

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 TimeoutException, NoSuchElementException

from selenium.webdriver.common.action_chains import ActionChains

driver = ["1", "2"]

colspan = ["1", "2"]

try:

out = open("类目.csv", "w", newline="")

except PermissionError:

print("文件被其他程序占用")

input("")

csv_writer = csv.writer(out, dialect="excel")

csv_writer.writerow(["宝贝ID", "类目"])

def open_chrome():

driver[0] = webdriver.Chrome()

driver[0].get("https://www.dianchacha.com")

input("请登陆后按回车:")

def EC_located(one_group, value):

"""

目的:简化代码长度,参数1选择one或者group切换选中模式

:param value:要找的值【CSS选择器】

:return:选择到的对象

"""

wait = WebDriverWait(driver[0], 10)

if one_group == "one":

try:

ecl = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, value)))

return ecl

except TimeoutException:

print(value, "1元素未加载成功,等待超时")

else:

try:

ecl = wait.until(

EC.presence_of_all_elements_located((By.CSS_SELECTOR, value))

)

return ecl

except TimeoutException:

print(value, "1元素---组---未加载成功,等待超时")

def operating(ID):

先获取ID输入框

driver[0].get("https://www.dianchacha.com/item/info/index/iid/" + ID)

html = driver[0].page_source

if "未能找到亲的宝贝" not in html:

colspans = EC_located("group", ".colspan-1")

colspan[0] = str(colspans[1].text).replace("宝贝类目: ", "")

else:

return operating(ID)

print(colspan)

def writer_txt():

csv_writer.writerow([url[0], colspan[0]])

print("保存", url[0], colspan[0], "成功")

url = ["0", "1"]

def main():

open_chrome()

file = "宝贝ID.txt"

with open(file) as f:

for line in f.readlines():

url[0] = line

print(line)

operating(url[0])

writer_txt()

out.close()

print("已完成")

if name == "main":

main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值