设为首页 加入收藏

TOP

爬取xx楼市随笔
2023-07-25 21:25:38 】 浏览:36
Tags:爬取

目标:爬取xx二手房楼市信息自己分析形势

目的:记录重温爬虫历程

1、引用包requests,random,time,pandas,bs4

2、查询:bs4使用方法,re。comple,以及他山之石。

3、思路:简单应用,直接写死不用def,class,

  -url 列表,初始列表后,append添加翻页url

  -headers,不加也可,网页写死的。

  -网页内数据写死的,爬网页即可,适合新手。

  -顺序:url-->-->空字典-->headers-->for-->bs4-->append添加字典列表-->pd.to-csv time.sleep(random.randint())

源代码:

import requests,random,time
import re
import pandas as pd
from bs4 import BeautifulSoup
shuju = {
"房子":[],
'房型':[],
'地址':[],
'面积':[],
'售价':[],
'单价':[],
'中介':[],
'更新时间':[]
}


url = ["https://xxxx"]
urllist= ['https://xxxx'+str(x) for x in range(2,17)]
headers = {
'User-Agent': 'xxxx.56'
}
for i in urllist:
  url.append(i)
gg=0

for xx in url:
  res = requests.get(url=xx,headers=headers)
  respone=BeautifulSoup(res.text,'html.parser')
  all_li = respone.find('ul',class_='item-list').find_all('li')
  for i in all_li:
    name1 = i.find('p',class_='title').find('a',target="_blank").text
    faxing = i.find('p',class_='detail').find('span').text
    address = i.find('span',class_='maps').text
    mianji = i.find('span',class_='area-detail_big').text
    money = i.find('em',class_='prices').text
    danjia = i.find('div',class_='content fl').find('div',class_='about-price').find('p',class_='tag').text
    zj = i.find('p',class_='agents').find('a').text
    uptime = i.find('p',class_='agents').find('span').text
    shuju["房子"].append(name1)
    shuju["房型"].append(faxing)
    shuju["地址"].append(address)
    shuju["面积"].append(mianji)
    shuju["售价"].append(money)
    shuju["单价"].append(danjia)
    shuju["中介"].append(zj)
    shuju["更新时间"].append(uptime)
    gg+=1
    print(danjia)
    time.sleep(random.randint(0,1))
    print('第{}条ok!'.format(gg))


df = pd.DataFrame(shuju)
df.to_csv('xxxx楼市二手房数据.csv',encoding='utf-8-sig')
print('爬完了')

 

 

以上注意缩进,怕忘备查。看得懂代码的都应知道了,我就不注释了。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python工具箱系列(二十四) 下一篇Python:对程序做性能分析及计时..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目