37,719
社区成员
发帖
与我相关
我的任务
分享
import requests
import bs4
import re
def find_data():
head = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
'Connection': 'keep-alive'}
res = requests.get('https://www.creprice.cn/rank/cityforsale.html',headers=head)
content = bs4.BeautifulSoup(res.text, "html.parser").find(class_="gb-dataListBox")
target = content.find_all("tr", style="cursor: pointer;")
info_list = []
for each in target:
tmp_dic = dict()
city = re.search('[^\x00-\xff]+',each.text).group()
price = re.search('\d+,\d+', each.text).group()
rate = re.findall('[+-]\d+.*%', each.text)
tmp_dic[city] = [price,rate[1],rate[0]]
info_list.append(tmp_dic)
print(info_list)
if __name__ == '__main__':
find_data()
[{'深圳': ['74,929', '+18.96%', '+2.86%']}, {'北京': ['62,567', '-2.09%', '-4.76%']}, {'上海': ['54,911', '+5.85%', '-0.25%']}, {'厦门': ['47,817', '+5.66%', '+0.27%']}, {'三亚': ['38,291', '+12.01%', '+3.72%']}, {'广州': ['35,934', '+6.13%', '+5.43%']}, {'杭州': ['31,487', '+4.1%', '+3.1%']}, {'南京': ['31,416', '+2.87%', '-0.24%']}, {'福州': ['26,288', '+0.55%', '+1.78%']}, {'天津': ['25,751', '+0.14%', '+1.4%']}, {'宁波': ['23,544', '+15.65%', '+0.5%']}, {'珠海': ['23,473', '+1.43%', '-0.37%']}, {'苏州': ['23,294', '+6.32%', '-1.96%']}, {'青岛': ['21,890', '+1.65%', '+0.76%']}, {'温州': ['21,777', '+7.11%', '-1.31%']}, {'丽水': ['19,428', '+7.9%', '-2.74%']}, {'武汉': ['18,942', '+4.89%', '+0.3%']}, {'东莞': ['17,921', '+11.79%', '+0.86%']}, {'金华': ['17,279', '+5.54%', '-0.69%']}, {'成都': ['16,726', '+7.34%', '+3.11%']}, {'无锡': ['16,675', '+12.46%', '+0.13%']}, {'合肥': ['16,500', '+4.93%', '-0.73%']},...., {'鹤岗': ['2,307', '-2.19%', '-2.92%']}]