这是源代码 下面的是报错信息 帮忙解决下 谢谢啦
import re
import json
import time
import requests
from selenium.webdriver import Chrome
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.by import By
from selenium.webdriver.support import ui, expected_conditions
from utils.header import get_ua
headers = {
'User-Agent': get_ua()
}
chrome = Chrome(executable_path='/Users/zkm/Desktop/envs/day04/chromedriver')
def get_allcity():
url = 'https://www.zhaopin.com/citymap'
resp = requests.get(url, headers=headers)
if resp.status_code == 200:
html = resp.text
s = re.search(r'<script>__INITIAL_STATE__=(.*?)</script>', html)
json_data = s.groups()[0]
data = json.loads(json_data)
cityMapList = data['cityList']['cityMapList'] # dict 字典类型
for letter, citys in cityMapList.items():
print(f'---{letter}---')
for city in citys:
"""
{
"name": "鞍山",
"url": "//www.zhaopin.com/anshan/",
"code": "601",
"pinyin": "anshan"
}
"""
yield city # 有yield 这个函数就变成生成器了
def get_city_jobs(url):
chrome.get(url) # 打开这个城市信息
# 根据class_name 查找webElement 这个是查找的搜索框
input_search: WebElement = chrome.find_element_by_class_name('zp-search__input')
input_search.send_keys('Python')
# 这个是点击搜索按钮
chrome.find_element_by_class_name('zp-search__btn').click()
time.sleep(2)
# 当前浏览器打开第二个窗口
w2 = chrome.window_handles[1]
chrome.switch_to.window(w2)
chrome.execute_script('var q=window.document.documentElement.scrollTop=3000')
time.sleep(0.2)
chrome.execute_script('var q=window.document.documentElement.scrollTop=5000')
time.sleep(0.2)
ui.WebDriverWait(chrome, 60).until(
expected_conditions.visibility_of_all_elements_located((By.CLASS_NAME, 'sou-main__list'))
)
# 判断当前查询的结果是否不存在
nocontent = chrome.find_element_by_class_name('page-empty__tips')
if not nocontent:
print('当前城市未查找到Python岗位')
else:
# 提取查找结果
divs = chrome.find_elements_by_class_name('joblist-box__item clearfix')
for div in divs:
# 每一个岗位
job_info_url = div.find_element(By.XPATH, './/a/@href')
print(job_info_url)
def get_city_jobs2(url):
chrome.get(url)
chrome.execute_script('var q=window.document.documentElement.scrollTop=3000')
time.sleep(0.2)
chrome.execute_script('var q=window.document.documentElement.scrollTop=5000')
time.sleep(0.2)
ui.WebDriverWait(chrome, 60).until(
expected_conditions.visibility_of_all_elements_located((By.CLASS_NAME, 'sou-main__list'))
)
#
# # 判断当前查询的结果是否不存在
nocontent = chrome.find_element_by_class_name('page-empty__tips')
if not nocontent:
print('当前城市未查找到Python岗位')
else:
# # 提取查找结果
divs = chrome.find_elements_by_class_name('joblist-box__item clearfix')
for div in divs:
# 每一个岗位
job_info_url = div.find_element(By.XPATH, './/a/@href')
print(job_info_url)
if __name__ == '__main__':
query_citys = ('北京', '北京', '北京')
for city in get_allcity():
# 可以保存city城市信息
# print(city)
# 请求城市下所有的python岗位
if city['name'] in query_citys:
get_city_jobs('https:'+city['url'])
# get_city_jobs2(f'https://sou.zhaopin.com/?jl={city["code"]}&kw=Python')
time.sleep(5)
这个就是代码出现问题的报错 上午还好好的运行 下午报这个错误搞的好难受啊 可以帮忙解决一下吗 谢谢大家啦