一个主界面文件代码,一个爬虫代码,主界面写好了框架,现在写好了文件,就差调用了
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
import time
from selenium.webdriver.common.by import By
class Yey:
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.get('https://map.baidu.com/@13523265.31,3641114.64,12z')
self.driver.implicitly_wait(5)
def parse(self):
self.driver.find_element_by_id('sole-input').send_keys('幼儿园')
time.sleep(1)
self.driver.find_element_by_id('search-button').click()
time.sleep(1)
self.driver.execute_script('window.scrollTO(0,document.body.scrollHeight)')
time.sleep(1)
li_list=self.driver.find_element_by_xpath('//div[@class="ml_30 mr_90"]/div/span')
for li in li_list:
name=li.find_element_by_xpath('./text')
print(name)
def main(self):
while True:
self.parse()
flag=self.driver.page_source.find('next next-none')
if flag==-1:
self.driver.find_element_by_tid('toNextPage').click()
if __name__ == '__main__':
y=Yey()
y.main()
import sys
import geopandas as gpd
from pyproj import Transformer
from PyQt5.QtWidgets import *
from PyQt5 import uic
ui_file = r"buffer.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(ui_file) # 加载ui界面文件
class Window(QMainWindow, Ui_MainWindow):
# 初始化界面实例
def __init__(self):
super().__init__()
self.setupUi(self)
# 函数名与qtdesigner里定义的槽函数一致
def search(self, event):
city_name = self.comboBox1.currentText() # 获取下拉列表中的城市名字
buffer_dist = float(self.lineEdit1.text()) # 获取输入框中的搜索距离,单位是km
source_city = gdf_prj[gdf_prj["城市名"] == city_name] # 找到shp中该条城市
point = source_city.iloc[0]["geometry"] # 获取到该城市的geometry
dist = gdf_prj.distance(point) # 查找所有城市到该城市的列表
gdf_prj["dist"] = dist # 将距离写入投影后的gdf
# 先按距离选择,然后排除源城市自身
selected_cities = gdf_prj[gdf_prj["dist"] <= buffer_dist * 1000]
selected_cities = selected_cities[selected_cities["城市名"] != city_name]
self.tableWidget1.setRowCount(len(selected_cities)) # 根据查询结果的条数,设置ui列表有多少行
# 将查询结果分别输入至ui列表中
for i in range(len(selected_cities)):
name = selected_cities.iloc[i]["城市名"]
distance = round(selected_cities.iloc[i]["dist"] * 0.001, 2)
newItem1 = QTableWidgetItem(name)
self.tableWidget1.setItem(i, 0, newItem1)
newItem2 = QTableWidgetItem(str(distance))
self.tableWidget1.setItem(i, 1, newItem2)
if not QApplication.instance():
app = QApplication(sys.argv)
else:
app = QApplication.instance()
window = Window()
# 读取shp在类和函数的外面定义,是为全局变量,所有类和方法都可以直接调用(但不可修改)。当然,写在函数里面也可以,即为局部变量,函数间调用可能不太方便。
gdf = gpd.read_file(r"china_cities.shp")
gdf.crs = 4326
crs = "+proj=aea +lat_1=25 +lat_2=47 +lat_0=0 +lon_0=105"
gdf_prj = gdf.to_crs(crs) # 转为投影坐标
# 依次写入下拉框备选项,其实直接用下拉框的addItems(series)也可以。
for i in range(len(gdf)):
city_name = str(gdf.iloc[i]["城市名"])
window.comboBox1.addItem(city_name)
window.lineEdit1.setText("300") # 设置缺省距离
window.tableWidget1.setColumnCount(2)
window.tableWidget1.setHorizontalHeaderLabels(["城市", "距离"])
window.tableWidget1.verticalHeader().setVisible(False)
window.show()
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>685</width>
<height>748</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QComboBox" name="comboBox1">
<property name="geometry">
<rect>
<x>210</x>
<y>110</y>
<width>161</width>
<height>41</height>
</rect>
</property>
<property name="font">
<font>
<family>黑体</family>
<pointsize>12</pointsize>
</font>
</property>
</widget>
<widget class="QPushButton" name="pushButton1">
<property name="geometry">
<rect>
<x>100</x>
<y>620</y>
<width>121</width>
<height>51</height>
</rect>
</property>
<property name="font">
<font>
<family>黑体</family>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>查找</string>
</property>
</widget>
<widget class="QLabel" name="label1">
<property name="geometry">
<rect>
<x>100</x>
<y>100</y>
<width>111</width>
<height>51</height>
</rect>
</property>
<property name="font">
<font>
<family>黑体</family>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>选择城市</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit1">
<property name="geometry">
<rect>
<x>210</x>
<y>180</y>
<width>161</width>
<height>41</height>
</rect>
</property>
<property name="font">
<font>
<family>黑体</family>
<pointsize>12</pointsize>
</font>
</property>
</widget>
<widget class="QLabel" name="label2">
<property name="geometry">
<rect>
<x>100</x>
<y>170</y>
<width>121</width>
你的下边的代码缺了一部分,环境问题我这也没法运行你的ui程序,假设两个py文件同目录,假设下边的文件名为spcode.py,然后在ui文件里加一行from spcode import Yey,然后就可以在任意地方使用了