free5man 2016-12-14 09:45 采纳率: 50%
浏览 2643

python代码pycharm报错

请问哪位知道这个代码是出了啥问题,最近刚接触python,这个代码主要是用来筛选符合条件的城市,在PyCharm上的代码显示如下:
from bs4 import BeautifulSoup
import requests
import re
from statistics import *
from pypinyin import lazy_pinyin
import time

def job_crawler(city, job):
url_zhaopin_main = 'http://sou.zhaopin.com/jobs/searchresult.ashx?jl={}&kw={}'.format(city, job)
web_data = requests.get(url_zhaopin_main)
soup = BeautifulSoup(web_data.text, 'lxml')
job_num = soup.select('body > div.main > div.search_newlist_main > div.search_yx > span > em')[0].get_text()
page_num = int(job_num)//60+1
if page_num > 90:
page_num = 90
salary_min = []
salary_max = []
for i in range(1, page_num+1):
url_zhaopin_page = url_zhaopin_main+'&p={}'.format(i)
web_data_page = requests.get(url_zhaopin_page)
soup_page = BeautifulSoup(web_data_page.text, 'lxml')
salary_total = soup_page.select('.zwyx')
for i in salary_total:
salary = i.get_text()
if salary.find('-')+1:
salary_min.append(int(salary[:salary.find('-')]))
salary_max.append(int(salary[salary.find('-')+1]))
average_salary_min = round(mean(salary_min))
average_salary_max = round(mean(salary_max))
median_salary_min = round(median(salary_min))
median_salary_max = round(median(salary_max))
print('在{},关键词为{}的工作共有{}份,平均工资为{}-{},中位数工资为{}-{}'.format(city, job, job_num, average_salary_min, average_salary_max, median_salary_min, median_salary_max))

def house_crawler(city):
city_pinyin = ''.join(lazy_pinyin(city))
for i in range(1, 20):
time.sleep(2)
rent_url = 'http://{}.ganji.com/fang1/o{}/'.format(city_pinyin,i)
rent_web_data = requests.get(rent_url)
rent_soup = BeautifulSoup(rent_web_data.text, 'lxml')
rent_prices = rent_soup.select('.sale-price')
rent_areas = rent_soup.find_all(text=re.compile("m²"))[1::2]
rent_price_list = []
rent_area_list = []
rent_sq_price_list = []
for price in rent_prices:
rent_price_list.append(price.get_text())
for area in rent_areas:
yuan_position = area.find('.')
rent_area_list.append(int(area[1:yuan_position]))
for i, j in zip(rent_price_list, rent_area_list):
if i != '面议' and j != 0:
rent_sq_price_list.append(round(int(i)/j))
rent_average_price = round(mean(rent_sq_price_list))
time.sleep(10)
buy_sq_price_list = []
for i in range(1, 20):
time.sleep(2)
buy_url = 'http://{}.ganji.com/fang5/o{}/'.format(city_pinyin,i)
buy_web_data = requests.get(buy_url)
buy_soup = BeautifulSoup(buy_web_data.text, 'lxml')
buy_sq_price = buy_soup.find_all(text=re.compile('元'))
for price in buy_sq_price:
if price != '':
if price[2:price.find('元')].isdigit():
buy_sq_price_list.append(int(price[2:price.find('元')]))
buy_price = round(mean(buy_sq_price_list))
print('{}的租房平均价格为{}元/平方米/月,二手房平均价格为{}元/平方米'.format(city, rent_average_price, buy_price))

def weather_crawler(city):
city_pinyin = ''.join(lazy_pinyin(city))
weather_url = 'http://lishi.tianqi.com/{}/index.html'.format(city_pinyin)
weather_web_data = requests.get(weather_url)
weather_soup = BeautifulSoup(weather_web_data.text, 'lxml')
weather = weather_soup.select('div.tqtongji>p')[0].get_text()[0:-15]
wind = weather_soup.select('div.tqtongji>ul')[1].get_text().replace('\n', '')
print(weather, '\n\n'+'风力情况为:\n', wind)
if name == '__main__':
city_keyword = input('请输入目标城市:')
job_keyword = input('请输入工作关键词:')
job_crawler(city_keyword, job_keyword)
print('')
house_crawler(city_keyword)
print('')
weather_crawler(city_keyword)

  • 写回答

1条回答 默认 最新

  • devmiao 2016-12-14 16:32
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条