python找房源_python抓取贝壳房源信息
分析了貝殼的房源信息數(shù)據(jù),發(fā)現(xiàn)地址鏈接的參數(shù)傳遞是有規(guī)律的
a3 實(shí)際表示的
l4 表示的是
然后 將復(fù)合條件拼成一個(gè)字符串,帶過去。看著真的很像加密過的。贊
import os, re
import requests
import random
import time
from bs4 import BeautifulSoup
user_agent_list = [
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1",
"Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6",
"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5",
"Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24",
"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24"
]
UA = random.choice(user_agent_list) ##從self.user_agent_list中隨機(jī)取出一個(gè)字符串
headers = {'User-Agent': UA} ##構(gòu)造成一個(gè)完整的User-Agent (UA代表的是上面隨機(jī)取出來的字符串哦)
url = 'https://tj.ke.com/chengjiao/pg'
for x in range(2,41):
time.sleep(random.randint(2,5))
with requests.get(url+str(x)+'ddo22p7', headers=headers, timeout=5) as response:
soup = BeautifulSoup(response.text, 'lxml')
# print(soup.title.text) # 打印title
li_list = soup.find('ul', class_='listContent').find_all('li')
# print(li_list)
for li_quick in li_list:
try:
#取成交日期
dealDate=li_quick.find('div',class_='dealDate').get_text().strip()
#取名稱
title=li_quick.find('div',class_='title').a.get_text().strip()
#取總價(jià)
totalPrice=li_quick.find('div',class_='totalPrice').find('span',class_='number').get_text().strip()
#取單價(jià)
unitPrice=li_quick.find('div',class_='unitPrice').find('span',class_='number').get_text().strip()
#取位置信息
positionInfo=li_quick.find('div',class_='positionInfo').get_text().strip()
#取成交周期
dealCycleTxt0=li_quick.find('div',class_='dealCycleeInfo').find('span',class_='dealCycleTxt').find_all('span')[0].get_text().strip()
#取掛牌價(jià)格
dealCycleTxt1=li_quick.find('div',class_='dealCycleeInfo').find('span',class_='dealCycleTxt').find_all('span')[1].get_text().strip()
except:
continue
finally:
print(title,',',dealDate,',',totalPrice,',',unitPrice,',',dealCycleTxt0,',',dealCycleTxt1,',',positionInfo)
總結(jié)
以上是生活随笔為你收集整理的python找房源_python抓取贝壳房源信息的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑wifi如何改密码忘了怎么办 电脑W
- 下一篇: 浅拷贝 python_python中什么