之前没学过爬虫,课题需要爬取上海市充电桩分布数据,根据别人的代码无法输出具体的经纬度。请问各位错在哪里?
import requests
import re
from pyquery import PyQuery as pq
from selenium import webdriver
browser = webdriver.Chrome(r'。。。')
kv = {'user-agent':'Mozilla/5.0'}
url = "http://admin.bjev520.com/jsp/beiqi/pcmap/do/pcMap.jsp?"
latter = "name=上海"
browser.get(url+latter)
browser.switch_to.frame('left') # iframe标签的name属性,最重要的一步
doc = pq(browser.page_source)
items = doc.find('.news-l .news-ul li a').items()
name = []
pos = []
urls = []
for item in items:
urls.append("http://admin.bjev520.com" + item.attr('href'))
name.append(item.find('p').text())
pos.append(item.find('span').text())
import json
from urllib.request import urlopen, quote
import requests
def getlnglat(address):
url = 'http://api.map.baidu.com/geocoding/v3/'
output = 'json'
ak = 'Iam9F3Gs6X03afWINMasrAdqCowF1cbx' # 百度地图ak,具体申请自行百度
address = quote(address) # 由于本文地址变量为中文,为防止乱码,先用quote进行编码
uri = url + '?' + 'address=' + address + '&output=' + output + '&ak=' + ak + '&callback=showLocation'
r = requests.get(uri)
tx = r.text
lng = re.findall(r'"lng":(\d+.\d+)', tx)
lat = re.findall(r'"lat":(\d+.\d+)', tx)
if lat:
return lat[0], lng[0] # 纬度 latitude , 经度 longitude ,
else:
return (0, 0)
print(lat[0],lng[0])