chenze0971 2022-01-08 14:46 采纳率: 85.7%
浏览 27
已结题

不知道这行代码的含义都是啥

全部代码如下:

import pandas as pd
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from time import sleep
import csv
# 这个爬虫爬取结果的最后几列需要手工处理(可能会多出来几列)
from selenium.webdriver.chrome.webdriver import WebDriver


def get_infos(ID):
    driver.get(r"http://192.168.3.252/xhlisweb-inspection_id/XHlisWebReport.aspx")
    sleep(1)
    driver.find_element_by_name("txtoutpatient_id").send_keys(ID)
    driver.find_element_by_name("btnConfirm").click()
    cols = driver.find_elements_by_xpath('''//tr[contains(@onclick, "return btnClick")]''')
    times = len(cols)

    # 思路:挨个去点击一行的病员号,然后获取下方表格的信息
    # print(driver.page_source)
    # cols = driver.find_elements_by_xpath("//td[text()=" + ID + "]")

    # col = cols[4]
    # col_info = col.text.split(' ')[:14]
    # col.click()
    # items = driver.find_elements_by_xpath("//div[@id='report-content']//tbody//tr")[1:]
    # item = items[0]

    infos = []
    for i in range(times):
        driver.get(r"http://192.168.3.252/xhlisweb-inspection_id/XHlisWebReport.aspx")
        sleep(2)
        driver.find_element_by_name("txtoutpatient_id").send_keys(ID)
        driver.find_element_by_name("btnConfirm").click()
        cols = driver.find_elements_by_xpath('''//tr[contains(@onclick, "return btnClick")]''')
        col = cols[i]
        col_info = col.text.split(' ')[:14]
        col.click()
        items = driver.find_elements_by_xpath("//div[@id='report-content']//tbody//tr")[1:]
        for item in items:
            a = item.text.split(' ')
            try:
                a.remove('')
            except:
                pass
            # 这里要做点长度判断,如果a的长度大于7,那就截断;如果不够,就填充''
            if len(a) <= 7:
                for i in range(7-len(a)):
                    a.append('')
            else:
                a = a[:7]
            infos.append([ID] + col_info + a)

    return infos


# start最小为0, end最大为641
start = 200
end = 641

data: object = pd.read_excel(r"C:\Users\cc\Desktop\资料\数据录入\ALL_raw.xlsx")
IDs = data['登记号'].tolist()[start:end]
# IDs = ["0005248871", '0010610644']


options = Options()
options.binary_location = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
s = Service(r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
driver = webdriver.Chrome(service=s)
driver.maximize_window()

ALL = []
for ID in IDs:

    try:
        infos = get_infos(ID)
        ALL += infos
    except:
        pass

headers = ['ID', '序号', '检验单', '病员号', '类型', '送检', '目的', '姓名', '性别', '年龄', '科别', '病区', '工作组', '审核人员', '审核日期', '审核时间', 'NO', '英文名称', '检验项目', '结果', '单位', '状态', '参考值']
with open(r"result_检验_" + str(start) + "_" + str(end) +".csv", 'w', newline='') as f:
    f_csv = csv.writer(f)
    f_csv.writerow(headers)
    for i in ALL:
        f_csv.writerow(i)


sleep(3)
driver.quit()


问题相关代码,请勿粘贴截图
headers = ['ID', '序号', '检验单', '病员号', '类型', '送检', '目的', '姓名', '性别', '年龄', '科别', '病区', '工作组', '审核人员', '审核日期', '审核时间', 'NO', '英文名称', '检验项目', '结果', '单位', '状态', '参考值']
with open(r"result_检验_" + str(start) + "_" + str(end) +".csv", 'w', newline='') as f:
    f_csv = csv.writer(f)
    f_csv.writerow(headers)
    for i in ALL:
        f_csv.writerow(i)

运行结果及报错内容

运行出来什么都没有,请问这每行代码都是什么意思

  • 写回答

2条回答 默认 最新

  • CSDN专家-HGJ 2022-01-08 16:22
    关注

    整个代码,是读取关键字ID,利用selenium模拟发送,请求数据,获取标签,解析数据,然后将结果写入csv中。没有获取到数据,先查看请求的页面有没有返回,即看url是否正确,是否需要在options中加入user-agent,或者cookie等参数,然后再检查节点标签选择,可使用print查验中间结果。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月16日
  • 已采纳回答 1月8日
  • 创建了问题 1月8日

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答