获取错误"pywintypes.error: (5, 'OpenClipboard', 'Access is denied.')"



我正在尝试将selenium与python一起使用,以从该站点获取纬度和经度。我也在使用win32lipboard。但每当我运行代码时,它都会随机地抛出这个错误pywintypes.error: (5, 'OpenClipboard', 'Access is denied.')

这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import csv
import win32clipboard
csvreader = csv.reader(open("master_data.csv", 'r'))
csvwriter = csv.writer(open('final_master_data.csv', 'w', newline=''))
headers = next(csvreader)
headers.append("latitude")
headers.append("longitude")
csvwriter.writerow(headers)
locations = list(csvreader)
chromedriver = 'C:\Users\UserName\Downloads\chromedriver.exe'
driver = webdriver.Chrome(chromedriver)
driver.get('http://www.whatsmygps.com')
for places in locations:
place = places[6] + ", " + places[4] + ", " + places[2]
location = driver.find_element_by_id("address")
location.send_keys(Keys.CONTROL, 'a')
location.send_keys(place)
location.submit()
time.sleep(3)
lat_input = driver.find_element_by_id("latitude")
lat_input.send_keys(Keys.CONTROL, 'a')
lat_input.send_keys(Keys.CONTROL, 'c')
win32clipboard.OpenClipboard()
lat = win32clipboard.GetClipboardData()
places.append(lat)
win32clipboard.CloseClipboard()
lon_input = driver.find_element_by_id("longitude")
lon_input.send_keys(Keys.CONTROL, 'a')
lon_input.send_keys(Keys.CONTROL, 'c')
win32clipboard.OpenClipboard()
lon = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
places.append(lon)
print(places)
csvwriter.writerow(places)
driver.close()

所以,每当我运行这段代码时,它一开始就没有问题,它读取csv文件并将位置名称输入到这些网站中,然后开始从网站复制纬度和经度,并将它们插入到另一个csv文件中。但一段时间后,它随机抛出错误pywintypes.error: (5, 'OpenClipboard', 'Access is denied.')。从昨天起,我就找不到解决这个问题的办法。

UPDATE:我正在使用Anaconda,并且我以管理员身份运行Anaconda shell,因此访问权限没有问题。

如果剪贴板被另一个进程锁定,则可能会发生拒绝访问错误。为了避免python消息,您可以使用WinAPI版本的剪贴板,如下SO链接所述:https://stackoverflow.com/a/23285159/4603670


作为替代方案,使用需要API密钥的BingMap。截至本文撰写之时,您可以在https://www.bingmapsportal.com免费API密钥(我不确定配额(。

import pythoncom
import win32com.client
import json
pythoncom.CoInitialize()
winhttp = win32com.client.Dispatch('WinHTTP.WinHTTPRequest.5.1')
def bing_find_gps(addressLine, postalCode, country):
q = 'http://dev.virtualearth.net/REST/v1/Locations?key='
q = q + 'my_api_key'
if country: q = q + '&countryRegion=' + country
if postalCode: q = q + '&postalCode=' + postalCode
if addressLine: q = q + '&addressLine=' + addressLine
try:
winhttp.Open('GET', q, False)
winhttp.Send()
if not winhttp.responseText:
return 0
list = json.loads(winhttp.responseText)
if list['statusCode'] != 200:
return 0
gps = list['resourceSets'][0]['resources'][0]['point']['coordinates']
if gps:
return (1, gps[0], gps[1])
except:
return 0
res = bing_find_gps('One Microsoft Way, Redmond, WA, 98052-6399', '0', 'United States')
if res:
print("lat/long %s, %s" % (res[1], res[2]))
res = bing_find_gps(0, '98052-6399', 'United States')
if res:
print("lat/long %s, %s" % (res[1], res[2]))


或使用openstreetmap.org:

address = "98052-6399" #Testing with Microsoft zip code
url = "https://nominatim.openstreetmap.org/search?format=json&q=" + address
winhttp.Open('GET', url, False)
winhttp.Send()
list = json.loads(winhttp.responseText)
print(list[0].get('lat'))
print(list[0].get('lon'))

预期输出:

Latitude: 47.670119 
Longitude: -122.118237

或者您也可能希望避免完全复制元素,使用get_attribute('value')读取latitudelongitude中的值。示例:

chromedriver = 'C:\Users\UserName\Downloads\chromedriver.exe'
driver = webdriver.Chrome(chromedriver)
driver.get('http://www.whatsmygps.com')
element = driver.find_element_by_id("address")
element.send_keys(Keys.CONTROL, 'a')
#enter Microsoft's zip code
element.send_keys('98052-6399')
element.submit()
time.sleep(3)
lat_input = driver.find_element_by_id("latitude")
print('latitude: ')
print(lat_input.get_attribute('value'))
lon_input = driver.find_element_by_id("longitude")
print('longitude: ')
print(lon_input.get_attribute('value'))
driver.close()

相关内容

  • 没有找到相关文章