爬取小猪短租网信息
admin
2023-07-13 01:23:22
0次
# -*- coding: utf-8 -*-
import time
import lxml
import requests
from bs4 import BeautifulSoup

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36',
          'Cookie': 'gr_user_id = c6f58a39 - ea25 - 4f58 - b448 - 545070192c4e;59a81cc7d8c04307ba183d331c373ef6_gr_session_id = e8e4b66f - 440a - 4ae7 - a76a - fe2dd2b34a26;59a81cc7d8c04307ba183d331c373ef6_gr_last_sent_sid_with_cs1 = e8e4b66f - 440a - 4ae7 - a76a - fe2dd2b34a26;59a81cc7d8c04307ba183d331c373ef6_gr_last_sent_cs1 = N % 2FA;59a81cc7d8c04307ba183d331c373ef6_gr_session_id_e8e4b66f - 440a - 4ae7 - a76a - fe2dd2b34a26 = true;grwng_uid = 9ec14ad9 - 5ac0 - 4bb1 - 81c1 - bc60d2685710;abtest_ABTest4SearchDate = b;xzuuid = 79426b52;_uab_collina = 154660443606130958890473;TY_SESSION_ID = 907f32df - c060 - 49ca - b945 - 98215cc03475;rule_math = pvzq3r06hi'}

def get_links(url):
    #dc = {}
    web_data = requests.get(url, headers = headers)
    soup = BeautifulSoup(web_data.text, 'lxml')
    #print(web_data.text)
    links = soup.select('#page_list > ul > li > a')
    for link in links:
        href = link.get('href')
        get_info(href)
        print(href)
        #a.append(href)
    #return str(dc)

def judgment_sex(class_name):
    if class_name == ['member_girl_ico']:
        return '女'
    elif class_name == ['member_boy_ico']:
        return '男'

def get_info(url):
    #data = {}
    wb_data = requests.get(url,headers = headers)
    soup = BeautifulSoup(wb_data.text,'html.parser')  #html.parser
    tittles = soup.select('body > div.wrap.clearfix.con_bg > div.con_l > div.pho_info > h5 > em')
    addresses = soup.select('body > div.wrap.clearfix.con_bg > div.con_l > div.pho_info > p > span')
    prices = soup.select('#pricePart > div.day_l > span')
    imgs = soup.select('#floatRightBox > div.js_box.clearfix > div.member_pic > a > img')
    names = soup.select('#floatRightBox > div.js_box.clearfix > div.w_240 > h7 > a')
    sexs = soup.select('#floatRightBox > div.js_box.clearfix > div.w_240 > h7 > span')
    '''
    print(tittles)
    print(addresses)
    print(prices)
    print(imgs)
    print(names)
    print(sexs)
    #print(prices.get_text())
    for price,name in zip(prices,names):
        print(price.get_text())
        print(name.get_text())
    #直接 print(prices.get_text()) 不写for报错
    for price in prices:
        print(type(price))
    print(type(prices))
    '''
    for tittle, address, price, img, name, sex in zip(tittles,addresses,prices,imgs,names,sexs):
        #print('ssk')
        data = {
            'tittle':tittle.get_text().strip(),
            'address':address.get_text().strip(),
            'price':price.get_text().strip(),
            'img':img.get("src"),
            'name':name.get_text().strip(),
            'sex':judgment_sex(sex.get("class"))
            }
        print(data)
    #return data
if __name__ == '__main__':
    urls = ['http://bj.xiaozhu.com/search-duanzufang-p{}-0/'.format(number) for number in range(1, 14)]
    for url in urls:
        get_links(url)
        print("------------------这里是1页分割线-----------------------")
        time.sleep(1)

    #f = open(r'C:\Users\PC\Desktop\file1.txt','a+', encoding="utf-8")
    #f.write(a)
    #f.close()
    #get_info('http://bj.xiaozhu.com/fangzi/29762014101.html')

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...