import pandas as pd
import re,requests,os
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'}
def get_pic(url,count=0,maxcount=10,code = 'utf-8'):
try:
r = requests.get(url,headers=headers,timeout=30)
r.raise_for_status()
r.encoding = code
return r.content
except:
count += 1
print('第{}次访问{}失败'.format(count,url))
if count < maxcount:
return get_pic(url,count=count)
house= pd.read_csv('bochao_choice_info.csv',usecols=['房屋地址','房间名称','海拔价','是否已租','配套设施','图片地址'])
# house=house[house["是否已租"]==0]
# # print(house)
# print(house['配套设施'])
#
# #none数字类型,无法in遍历,先转换成str类型
# house["is_duwei"] = house['配套设施'].apply(lambda x : '独卫' in f'{x}')
# house = house[house["is_duwei"] == True]
# print(house)
districts=["开福区","芙蓉区","雨花区","天心区","长沙县","岳麓区","望城区"]
for district in districts:
root = f'./博超公寓照片/{district}/'
if not os.path.exists(root):
os.mkdir(root)
house["is_need"] = house.apply(lambda x : district in x['房屋地址'],axis = 1)
infos = house[house["is_need"] == True]
print(infos)
quyu=infos['房屋地址']
address=infos['房间名称']
price = infos['海拔价']
fac = infos['配套设施']
pic=infos['图片地址']
order=range(1,len(quyu))
# top = """
# <!DOCTYPE html>
# <html lang="en">
# <head>
# <meta charset="UTF-8">
# <title>个人房源更新</title>
# </head>
# <body>
# """
# bottom = '''</body>
# </html>'''
# path = f'公众号信息发布内容{district}.html'
# with open(path, 'w',encoding='utf-8') as f:
# f.write(top)
for quyu,address,price,pics,order,fac in zip(quyu,address,price,pic,order,fac):
pics = re.findall('(http.*?g)\?',str(pics))
# print(pics)
# info = """
# <p>{}.#{}-房东直租</p>
# <p>地址:{}</p>
# <p>小区:{}</p>
# <p>价格:{} 押一付一,可短租,无中介费</p>
# <p>配套设施:{}</p>
# <p>照片:看房联系平台小编微信:17752882209</p>
# <p>
# """.format(order,district,quyu,address,price,fac)
# info2 = ''
num=0
for pic in pics:
num += 1
r = get_pic(pic)
room_path = f'{address}{price}图片{num}.jpg'.replace('/',"")
with open(root+room_path,'wb') as f:
f.write(r)
print('{}已下载完成'.format(room_path))
下载保存图片
最新推荐文章于 2022-12-04 16:09:51 发布