get_price.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # -*- coding: utf-8 -*-
  2. # @Author : ChenZhaoyuchen
  3. # @Time : 2024/9/27 16:17
  4. # @File : get_price.py
  5. import requests
  6. from anjuke.utils.get_message import *
  7. from anjuke.utils.setting import *
  8. def get_price():
  9. # 价格列表
  10. periphery_name_list = get_periphery()[0]
  11. periphery_url_list = get_periphery()[1]
  12. # 主函数
  13. result = []
  14. for i in range(len(periphery_name_list)):
  15. name = periphery_name_list[i]
  16. url = periphery_url_list[i]
  17. # url = 'https://www.anjuke.com/fangjia/beijing2024/chaoyang/'
  18. response_price = requests.get(url = url, headers = headers).content.decode('utf8')
  19. soup = BeautifulSoup(response_price, 'html.parser')
  20. price_ = soup.find(class_ = "table is-headless")
  21. table_trs = price_.find_all('div',class_ = "table-tr")
  22. # html提取 时间time,单价price,涨跌tab,涨跌比率Price_Rate
  23. list1 = []
  24. for table_tr in table_trs:
  25. all_mes = table_tr.find_all('div',class_ = 'td')
  26. zhongji = []
  27. for td in all_mes:
  28. a = td.text.strip() # 防止出现换行符等符号错误
  29. print('a',a)
  30. # 每轮3个a值,包括时间time,单价price,涨跌比率Price_Rate
  31. zhongji.append(a)
  32. UporDown = table_tr.find(
  33. 'div', class_="up") or table_tr.find('div', class_="down")
  34. attr = UporDown['class'][0]
  35. if attr == "up":
  36. tab = 'UP'
  37. elif attr == "down":
  38. tab = 'DOWN'
  39. else:
  40. tab = 'CONSTANT'
  41. zhongji.insert(2,tab)
  42. list1.append(zhongji)
  43. list1.append(name)
  44. result.append(list1)
  45. return result
  46. # if __name__ == '__main__':
  47. # connectMysql()
  48. # get_price()
  49. # closeMysql(conn = )