51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 925|回复: 3
打印 上一主题 下一主题

用python爬取数据出错

[复制链接]
  • TA的每日心情
    无聊
    4 天前
  • 签到天数: 406 天

    连续签到: 3 天

    [LV.9]测试副司令

    跳转到指定楼层
    1#
    发表于 2022-1-7 09:55:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    1测试积点
    1. import pandas as pd
    2. from selenium import webdriver
    3. from selenium.webdriver.chrome.options import Options
    4. from time import sleep
    5. import csv
    6. # 这个爬虫爬取结果的最后几列需要手工处理(可能会多出来几列)
    7. def get_infos(ID):
    8.     driver.get(r"http://192.168.3.252/xhlisweb-inspection_id/XHlisWebReport.aspx")
    9.     sleep(1)
    10.     driver.find_element_by_name("txtoutpatient_id").send_keys(ID)
    11.     driver.find_element_by_name("btnConfirm").click()
    12.     cols = driver.find_elements_by_xpath('''//tr[contains(@onclick, "return btnClick")]''')
    13.     times = len(cols)
    14.     # 思路:挨个去点击一行的病员号,然后获取下方表格的信息
    15.     # print(driver.page_source)
    16.     # cols = driver.find_elements_by_xpath("//td[text()=" + ID + "]")
    17.     # col = cols[4]
    18.     # col_info = col.text.split(' ')[:14]
    19.     # col.click()
    20.     # items = driver.find_elements_by_xpath("//div[@id='report-content']//tbody//tr")[1:]
    21.     # item = items[0]
    22.     infos = []
    23.     for i in range(times):
    24.         driver.get(r"http://192.168.3.252/xhlisweb-inspection_id/XHlisWebReport.aspx")
    25.         sleep(2)
    26.         driver.find_element_by_name("txtoutpatient_id").send_keys(ID)
    27.         driver.find_element_by_name("btnConfirm").click()
    28.         cols = driver.find_elements_by_xpath('''//tr[contains(@onclick, "return btnClick")]''')
    29.         col = cols[i]
    30.         col_info = col.text.split(' ')[:14]
    31.         col.click()
    32.         items = driver.find_elements_by_xpath("//div[@id='report-content']//tbody//tr")[1:]
    33.         for item in items:
    34.             a = item.text.split(' ')
    35.             try:
    36.                 a.remove('')
    37.             except:
    38.                 pass
    39.             # 这里要做点长度判断,如果a的长度大于7,那就截断;如果不够,就填充''
    40.             #if len(a) <= 7:
    41.             #    for i in range(7-len(a)):
    42.             #        a.append('')
    43.             #else:
    44.             #    a = a[:7]
    45.             infos.append([ID] + col_info + a)
    46.     return infos

    47. # start最小为0, end最大为641
    48. start = 200
    49. end = 641
    50. data = pd.read_excel(r"C:\Users\cc\Desktop\资料\数据录入\ALL_raw.xlsx")
    51. IDs = data['登记号'].tolist()[start:end]
    52. # IDs = ["0005248871", '0010610644']
    53. options = Options()
    54. options.binary_location = r"C:\Users\newceshi\Desktop\蒋丽莎病历检查\pzwj\google\chrome.exe"
    55. driver = webdriver.Chrome(r"C:\Users\newceshi\Desktop\蒋丽莎病历检查\pzwj\chromedriver.exe", chrome_options=options)
    56. driver.maximize_window()
    57. ALL = []
    58. for ID in IDs:
    59.     try:
    60.         infos = get_infos(ID)
    61.         ALL += infos
    62.     except:
    63.         pass
    64. headers = ['ID', '序号', '检验单', '病员号', '类型', '送检', '目的', '姓名', '性别', '年龄', '科别', '病区', '工作组', '审核人员', '审核日期', '审核时间', 'NO', '英文名称', '检验项目', '结果', '单位', '状态', '参考值']
    65. with open(r"result_检验_" + str(start) + "_" + str(end) +".csv", 'w', newline='') as f:
    66.     f_csv = csv.writer(f)
    67.     f_csv.writerow(headers)
    68.     for i in ALL:
    69.         f_csv.writerow(i)

    70. sleep(3)
    71. driver.quit()
    复制代码
    运行结果及报错内容
    1. C:\Users\cc\AppData\Local\Programs\Python\Python39\python.exe D:/Pycharm/data/chaxue4.py
    2. Traceback (most recent call last):
    3.   File "D:\Pycharm\data\chaxue4.py", line 58, in <module>
    4.     data = pd.read_excel(r"C:\Users\cc\Desktop\资料\数据录入\ALL_raw.xlsx")
    5.   File "C:\Users\cc\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    6.     return func(*args, **kwargs)
    7.   File "C:\Users\cc\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\excel\_base.py", line 364, in read_excel
    8.     io = ExcelFile(io, storage_options=storage_options, engine=engine)
    9.   File "C:\Users\cc\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\excel\_base.py", line 1233, in __init__
    10.     self._reader = self._engines[engine](self._io, storage_options=storage_options)
    11.   File "C:\Users\cc\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\excel\_openpyxl.py", line 521, in __init__
    12.     import_optional_dependency("openpyxl")
    13.   File "C:\Users\cc\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\compat\_optional.py", line 118, in import_optional_dependency
    14.     raise ImportError(msg) from None
    15. ImportError: Missing optional dependency 'openpyxl'.  Use pip or conda to install openpyxl.
    16. Process finished with exit code 1
    复制代码
    我的解答思路和尝试过的方法

    我从别人那搞到的代码,但我电脑上运行的结果是这样,看不懂什么意思


    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

  • TA的每日心情
    开心
    5 天前
  • 签到天数: 1001 天

    连续签到: 1 天

    [LV.10]测试总司令

    2#
    发表于 2022-1-10 09:44:07 | 只看该作者
    ImportError: Missing optional dependency 'openpyxl'.  Use pip or conda to install openpyxl.
    问题出在这。
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    4 天前
  • 签到天数: 1389 天

    连续签到: 3 天

    [LV.10]测试总司令

    3#
    发表于 2022-1-10 11:07:30 | 只看该作者
    下断点吧,一步一步调试
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    4 天前
  • 签到天数: 2663 天

    连续签到: 3 天

    [LV.Master]测试大本营

    4#
    发表于 2022-1-10 14:34:34 | 只看该作者
    缺少依赖?
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-5-4 16:30 , Processed in 0.072190 second(s), 21 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表