-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_asyinc.py
159 lines (145 loc) · 9.79 KB
/
test_asyinc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import asyncio
import aiohttp
import time
from lxml import etree
# from itertools import product
from tqdm import tqdm
import time
import pandas as pd
import numpy as np
import random
import json
# from utils import verbose_exception,create_headers
# import re
import logging
logging.basicConfig(filename='app.log',
level=logging.INFO,
filemode="w",
format='%(asctime)s - %(levelname)s - %(lineno)d - %(module)s - %(funcName)s - %(message)s')
TIMEOUT = 100
USER_AGENTS = [
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)",
"Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
"Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)",
"Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)",
"Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)",
"Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0",
"Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5",
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20",
"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52",
]
async def get_pages(urls,max_concurrency=30,ban_kuai="",semaphore=None):
s = time.time()
results_ = []
dit = {
'标题': None,
'卖点': None,
'总价': None,
'单价': None,
'户型': None,
'楼层': None,
'共有楼层数': None,
'装修': None,
'朝向': None,
'建造时间': None,
'面积': None,
'小区': None,
'区域': None,
"关注": None,
'房屋属性': None,
'详情页': None,}
async with semaphore:
async with aiohttp.ClientSession() as session:
for url in urls:
headers = {"User-Agent":random.choice(USER_AGENTS),
"Referer":"https://sh.lianjia.com/yangjing",
# "Accept-Encoding":"gzip, deflate, br, zstd",
# "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
# "Connection":"keep-alive",
# "Host":"sh.lianjia.com",
# "Accept-Language":"zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
# "Cookie":"lianjia_uuid=ddbe60c3-74a2-44bb-a86d-98e72b1706c3; _smt_uid=665e8096.1d5e6a01; Hm_lvt_9152f8221cb6243a53c83b956842be8a=1717469334; _jzqc=1; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2218fe1264c3edb1-0124604a9f69e5-4c657b58-2073600-18fe1264c3f3025%22%2C%22%24device_id%22%3A%2218fe1264c3edb1-0124604a9f69e5-4c657b58-2073600-18fe1264c3f3025%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%7D; _ga=GA1.2.1265665742.1717469336; _gid=GA1.2.2007536876.1717469336; _ga_654P0WDKYN=GS1.2.1717469336.1.0.1717469336.0.0.0; _qzjc=1; select_city=310000; _jzqa=1.2325446619701753300.1717469334.1717475770.1717560778.3; _jzqckmp=1; Hm_lpvt_9152f8221cb6243a53c83b956842be8a=1717564925; _qzja=1.1933495970.1717469365462.1717475770299.1717560778123.1717564316016.1717564925578.0.0.0.23.3; _qzjto=11.1.0; srcid=eyJ0Ijoie1wiZGF0YVwiOlwiNDllZGM3ZTk3NmFiN2MzYzkwMDdiOTczMDUzOTM2ZjkzZWNhOGMyYjRiNzI5ZWI2YjQxOGEyNzc0MjE2YWZhMTViMGZjNDMwMTFjYTU4MWJiNWY4Y2MyMTRiZGQ0MTdlNTVkYjE2N2IzOGY5ZWJjNjM4N2U2OGYzYWU1Y2I5ZWI2ODY4ZDQyZDRiNjFmNzczOTQwYjRjMTkxZDg1MDAzNWQwMWE3ODM4ZmQzNWFkMTg1YTk2NjQ0YzMyYmZhOTQ5NDA1MjNhYmUwZjI5MjA0YzgwZDA4NTg3OTNhZTZiN2VjNmM4ZTU0NzBlNjNhNmNiMjJkYjBjMjY3ZTQyNmRkOVwiLFwia2V5X2lkXCI6XCIxXCIsXCJzaWduXCI6XCI3Y2FiMmZlYVwifSIsInIiOiJodHRwczovL3NoLmxpYW5qaWEuY29tL2Vyc2hvdWZhbmcvZ2FvaGFuZy9wZzM1LyIsIm9zIjoid2ViIiwidiI6IjAuMSJ9; _ga_LRLL77SF11=GS1.2.1717561392.3.1.1717564933.0.0.0; _ga_GVYN2J1PCG=GS1.2.1717561392.3.1.1717564933.0.0.0"
}
async with session.get(url,
headers=headers,
timeout=TIMEOUT,) as resp:
# await asyncio.sleep(10)
html = await resp.text()
if resp.status != 200:
logging.error(f"{resp.status}")
logging.info(f'{url}')
try:
root = etree.HTML(html)
eles = root.xpath('//ul[@class="sellListContent"]/li/div[@class="info clear"]')
title = [next(iter(ele.xpath("div[@class='title']/a/text()")),"None") for ele in eles]
selling_point = [next(iter(ele.xpath("div[@class='tag']/span[@class='taxfree']/text()")),"None") for ele in eles]
price = [next(iter(ele.xpath("div[@class='priceInfo']/div[1]/span/text()")),"None") for ele in eles]
unitPrice = [next(iter(ele.xpath("div[@class='priceInfo']/div[2]/span/text()")),"None") for ele in eles]
house_infos_ = [next(iter(ele.xpath("div[@class='address']/div[@class='houseInfo']/text()")),"None") for ele in eles]
house_infos_aft = [it.split("|") for it in house_infos_ ]
house_infos_aft_new = []
#修正缺失导致的错误问题
for it in house_infos_aft:
if len(it) < 7:
house_infos_aft_new.append(it + ['None' for _ in range(7 - len(it))])
else:
house_infos_aft_new.append(it[:7])
house_infos = np.array(house_infos_aft_new,dtype=object)
# print(house_infos)
house_type = house_infos[:,0]
area = [a.replace("平米","")for a in house_infos[:,1]]
face = house_infos[:,2]
# print(face)
furnish = house_infos[:,3]
floor = house_infos[:,4]
num = house_infos[:,4]
date = house_infos[:,5]
houseProperty = house_infos[:,6]
community = [next(iter(ele.xpath("div[@class='flood']/div[@class='positionInfo']/a[1]/text()")),"None") for ele in eles]
region = [next(iter(ele.xpath("div[@class='flood']/div[@class='positionInfo']/a[2]/text()")),None) for ele in eles]
follow = [next(iter(ele.xpath("div[@class='followInfo']/text()")),"None") for ele in eles]
link = [next(iter(ele.xpath("div[@class='title']/a/@href")),"None") for ele in eles]
result = {
'标题': title,
'卖点': selling_point,
'总价': price,
'单价': unitPrice,
'户型': house_type,
'楼层': floor,
'共有楼层数': num,
'装修': furnish,
'朝向': face,
'建造时间': date,
'面积': area,
'小区': community,
'区域': region,
"关注": follow,
'房屋属性': houseProperty,
'详情页': link,
}
except Exception as e:
result = dit.copy()
results_.append(result.copy())
# print(results_)
logging.info(results_)
try:
df = pd.concat([pd.DataFrame(result) for result in results_])
df.to_excel(f"outputs/output__{ban_kuai}.xlsx",index=False)
except Exception as e:
logging.error(e)
logging.info(f"get_pages Total time cost: {time.time() - s}")
if __name__ == "__main__":
province_code = "sh" #shanghai
max_concurrency = 20
semaphore = asyncio.Semaphore(500)
# urls_json = "fruits.json"
urls = [f"https://sh.lianjia.com/ershoufang/yangjing/pg{i}/" for i in range(1,6) ]
print(urls)
asyncio.run(get_pages(urls,max_concurrency=max_concurrency,ban_kuai="yangjing",semaphore=semaphore))