黄昏之鸟 2021-04-10 11:24 采纳率: 66.7%
浏览 97
已采纳

python爬虫实例小猪短租爬虫,运行后爬不到数据,求各位老板指点

import requests
from bs4 import BeautifulSoup
import time
headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 '
                 '(KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}
def judgment_sex(class_name):#判别房东男女的函数
    if class_name==['member_ico1']:
        return '女'
    else:
        return '男'


def get_links(url):#获取详细页
    wb_data = requests.get(url, headers=headers)
    soup = BeautifulSoup(wb_data.text, 'lxml')
    links = soup.select('#page_list > ul > li> a ')
    for link in links:
        href = link.get('href')
        get_info(href)

def get_info(url):#获取网页信息的函数
    wb_data=requests.get(url,headers=headers)
    soup=BeautifulSoup(wb_data.text,'lxml')
    tittles=soup.select('div.pho_info > h4')
    addresses=soup.select('span.pr5')
    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 > h6 > a')
    sexs=soup.select('#floatRightBox > div.js_box.clearfix > div.member_pic > div')
    for tittle,address,price,img,name,sex in zip(tittles,addresses,prices,imgs,names,sexs):
        data={
            'tittle':tittle.get_text().strip(),
            'address':address.get_text().strip(),
            'price':price.get_text(),
            'img':img.get("src"),
            'name':name.get_text(),
            'sex':judgment_sex(sex.get("class"))
        }
    print(data)#打印信息


if __name__ == '__main__':#主入口
    urls=['https://gz.xiaozhu.com/search-duanzufang-p{}-0/'.format(number) for number in range(1,4)]
    for single_url in urls:
        get_links(single_url)
        time.sleep(2)#睡眠2秒

  • 写回答

2条回答 默认 最新

  • 木三136 2021-04-10 11:40
    关注

    该网站是使用ajax动态加载的,您使用get_links函数所获得的的网页源码是不完整的,所以什么都爬不到

    您可以通过获取json文件来获得您需要的内容

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?