栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

使用BeautifulSoup解析爬取安居客租房信息存入MySQL

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用BeautifulSoup解析爬取安居客租房信息存入MySQL

一.前言

学习爬虫数据解析以及存入MySQL的小练习

二. 结果展示

三.爬取网页
https://hangzhou.anjuke.com/sale/p15/?from=navigatio
四.源代码
import requests
from bs4 import BeautifulSoup
import mysql.connector

class ZufangSpider():
    my_db = mysql.connector.connect(host='localhost', user='root', passwd='root', database='mytestdb',
                                    auth_plugin='mysql_native_password')
    mycursor = my_db.cursor()

    def __init__(self):
        self.url='https://hangzhou.anjuke.com/sale/p15/?from=navigation'
        self.headers={'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'}

    def send_requests(self,url):
        resp=requests.get(url,headers=self.headers)
        if resp.status_code==200:
            return resp

    def parse_html(self,resp):
        lst=[]
        html=resp.text
        bs=BeautifulSoup(html,'lxml')
        section=bs.find('section',class_='list')
        div_list=section.find_all('div',class_='property-content')
        for item in div_list:
            titles=item.find('h3',class_='property-content-title-name').text
            content=item.find('div',class_='property-content-info')
            p_list=content.find_all('p')
            houseinfo=p_list[0].text
            area=p_list[1].text
            areas=area[28:34]
            floor=p_list[3].text
            floors=floor[28:37]
            year=p_list[4].text
            years=year[28:36]
            name=item.find('p',class_='property-content-info-comm-name').text
            address=item.find('p',class_='property-content-info-comm-address').text
            price=item.find('span',class_='property-price-total-num').text
            prices=price+'万'
            price_average=item.find('p',class_='property-price-average').text
            sale_man=item.find('span',class_='property-extra-text').text
            lst.append((titles,houseinfo,areas,floors,years,name,address,prices,price_average,sale_man))
        self.save(lst)

    def save(self,lst):
        sql='insert into tb_hangzhou (titles,houseinfo,areas,floors,years,nname,address,prices,price_average,sale_man) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'
        self.mycursor.executemany(sql,lst)
        self.my_db.commit()
        print('插入完毕')


    def start(self):
        url=self.url
        resp=self.send_requests(url)
        self.parse_html(resp)

if __name__ == '__main__':
    zufang=ZufangSpider()
    zufang.start()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/618872.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号