selenium python怎么读_如何在Python中读取Selenium Webdriver下载的文件

1586010002-jmsa.png

I am using selenium with webdriver in python to download a csv file from a site . The file gets downloaded into the download directory specified. Here is an overview of my code

fp = webdriver.FirefoxProfile()

fp.set_preference("browser.download.folderList", 2)

fp.set_preference("browser.download.manager.showWhenStarting", False)

fp.set_preference("browser.download.dir",'xx/yy')

fp.set_preference('browser.helperApps.neverAsk.saveToDisk', "text/plain, application/vnd.ms-excel, text/csv, text/comma-separated-values, application/octet-stream")

driver = webdriver.Firefox(fp)

driver.get('url')

I need to print the contents of this csv to the terminal . A lot of similar files with random names will be downloaded into the same folder so accessing the file via filename wont work as I don't know what it will be in advance

解决方案

This answer was formed from a combination of previous stack overflow questions , answers as well as comments in this post so thank you everyone.

I combined selenium webdriver and the python requests module for this solution . I essentially logged into the site using selenium, copied the cookies from the webdriver session and then used a requests.get(url,cookies = webdriver_cookies) to get the file.

Here's the gist of my solution

fp = webdriver.FirefoxProfile()

fp.set_preference("browser.download.folderList", 2)

fp.set_preference("browser.download.manager.showWhenStarting", False)

fp.set_preference("browser.download.dir",'xx/yy')

fp.set_preference('browser.helperApps.neverAsk.saveToDisk', "text/plain, application/vnd.ms-excel, text/csv, text/comma-separated-values, application/octet-stream")

driver = webdriver.Firefox(fp)

# selenium login code ...

driver_cookies = driver.get_cookies()

cookies_copy = {}

for driver_cookie in driver_cookies:

cookies_copy[driver_cookie["name"]] = driver_cookie["value"]

r = requests.get('url',cookies = cookies_copy)

print r.text

I hope that this helps someone

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值