python抓取贝壳房源信息

分析了贝壳的房源信息数据,发现地址链接的参数传递是有规律的

https://tj.ke.com/chengjiao/a3l4/

a3 实际表示的 

l4 表示的是

 

 然后 将复合条件拼成一个字符串,带过去。看着真的很像加密过的。赞

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中随机取出一个字符串
headers = {'User-Agent': UA}  ##构造成一个完整的User-Agent (UA代表的是上面随机取出来的字符串哦)

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()
                #取总价
                totalPrice=li_quick.find('div',class_='totalPrice').find('span',class_='number').get_text().strip()
                #取单价
                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()
                #取挂牌价格
                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)

  

posted on 2019-09-15 16:03  耀扬  阅读(2590)  评论(0编辑  收藏  举报

导航