利用BeautifulSoup爬去我爱我家的租房数据
因为之前对BeautifulSoup一直不是很熟悉,刚好身边的朋友同事在找房子,就想着能不能自己写个爬虫爬一下数据,因此就写了这个爬虫。基本都是边看书边写的,不过也没什么好讲的。直接粘代码了。
# coding=utf-8
import requests
from bs4 import BeautifulSoup
import pymysql
import time
db= pymysql.connect(host="127.0.0.1",port =3306,user="root" ,passwd="root",db="woaiwojia",charset='utf8')
cursor = db.cursor()
for num in range(1,81):
url = "https://sh.5i5j.com/zufang/o8r1u1n"+str(num)+"/"
time.sleep(10)
strhtml = requests.get(url)
fanlist = BeautifulSoup(strhtml.text,"lxml")
sthtml = fanlist.find_all("ul",{"class":"pList"})
for ul in fanlist.find_all("ul",{"class":"pList"}):
for li in ul.find_all(name="li"):
for div in li.find_all("div",{"class":"listCon"}):
xiaoqu = div.h3.a.string
detailUrl = "https://sh.5i5j.com"+div.h3.a.attrs['href']
detailhtml = requests.get(detailUrl)
detail = BeautifulSoup(detailhtml.text,"lxml")
jinjirenlist =detail.find_all("div",{"id":"housebroker"})
for div1 in div.find_all("div",{"class":"listX"}):
area = div1.find_all("p")[0].text
community = div1.find_all("p")[1].text
hot = div1.find_all("p")[2].text
price = div1.find_all("div",{"class":"jia"})[0].p.strong.string
for uldiv in detail.find_all("div",{"id":"housebroker"}):
for ul in uldiv.find_all("ul"):
lxrphone = ul.h3.string+ul.label.string
sql = "insert into zufang(area,xiaoqu,community,hot,price,lxrphone) VALUES ('%s','%s','%s','%s','%s','%s');" % (area, xiaoqu,community,hot,price,lxrphone)
try:
cursor.execute(sql)
db.commit()
except:
print('插入失败')
有什么问题或者建议可以评论与我进行交流
利用BeautifulSoup爬去我爱我家的租房数据的更多相关文章
- python爬虫:利用BeautifulSoup爬取链家深圳二手房首页的详细信息
1.问题描述: 爬取链家深圳二手房的详细信息,并将爬取的数据存储到Excel表 2.思路分析: 发送请求--获取数据--解析数据--存储数据 1.目标网址:https://sz.lianjia.com ...
- Python爬虫之利用BeautifulSoup爬取豆瓣小说(二)——回车分段打印小说信息
在上一篇文章中,我主要是设置了代理IP,虽然得到了相关的信息,但是打印出来的信息量有点多,要知道每打印一页,15个小说的信息全部会显示而过,有时因为屏幕太小,无法显示全所有的小说信息,那么,在这篇文章 ...
- 爬虫--scrapy+redis分布式爬取58同城北京全站租房数据
作业需求: 1.基于Spider或者CrawlSpider进行租房信息的爬取 2.本机搭建分布式环境对租房信息进行爬取 3.搭建多台机器的分布式环境,多台机器同时进行租房数据爬取 建议:用Pychar ...
- Python 利用 BeautifulSoup 爬取网站获取新闻流
0. 引言 介绍下 Python 用 Beautiful Soup 周期性爬取 xxx 网站获取新闻流: 图 1 项目介绍 1. 开发环境 Python: 3.6.3 BeautifulSoup: ...
- Python爬虫之利用BeautifulSoup爬取豆瓣小说(一)——设置代理IP
自己写了一个爬虫爬取豆瓣小说,后来为了应对请求不到数据,增加了请求的头部信息headers,为了应对豆瓣服务器的反爬虫机制:防止请求频率过快而造成“403 forbidden”,乃至封禁本机ip的情况 ...
- pyhton 爬虫爬去吾爱精品软件的信息并写入excel
2018的最后一天了,感觉今年有得有失,这里就不再浪费时间了,愿2019万事如意 之前的爬虫信息下载后只写入txt文档,想到了以后工作加入有特殊需求,趁放假有时间将这写数据写入excel表格 以吾爱精 ...
- <爬虫>利用BeautifulSoup爬取百度百科虚拟人物资料存入Mysql数据库
网页情况: 代码: import requests from requests.exceptions import RequestException from bs4 import Beautiful ...
- 利用Python爬去囧网福利(多线程、urllib、request)
import os; import urllib.request; import re; import threading;# 多线程 from urllib.error import URLErro ...
- Python爬虫之利用BeautifulSoup爬取豆瓣小说(三)——将小说信息写入文件
#-*-coding:utf-8-*- import urllib2 from bs4 import BeautifulSoup class dbxs: def __init__(self): sel ...
随机推荐
- Unity3D 连接MySql
MySql安装如下: https://www.cnblogs.com/dlvguo/p/9671832.html Unity连接MySql首先要在Unity的安装目录D:\Unity2017\Edit ...
- html script生成二维码
<div class="code" align="center"> <p >手机端扫描以下二维码直接观看(支持安卓Android/苹果i ...
- 记一次Python pip安装失败的总结
pip 安装失败时,可能换此方法可解决1.升级pip版本,这个一般会主动提示python3 -m pip install --upgrade pip2.修改pip源,默认的pip源速度实在无法忍受,或 ...
- PHP Memcache 扩展安装
PHP Memcache 扩展安装 PHP Memcache 扩展包下载地址:http://pecl.php.net/package/memcache,你可以下载最新稳定包(stable). 下载对应 ...
- js swich
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Dapper基本使用
http://www.cnblogs.com/Sinte-Beuve/p/4231053.html
- php php-fpm、nginx和js
1 php-fpm是什么 php-fpm是php fastCGI process manager的缩写.它是php的进程管理器,对每个请求的处理都是一个进程. php-fpm管理了一个进程池,假如进程 ...
- c语言中不允许在函数外部给全局变量赋值
今天,在写条件编译的时候,出现了在函数外部给全局变量赋值的情况,gcc报错,那么c语言为什么不允许在函数外部给变量赋值呢?为什么声明变量的时候可以对变量进行赋值? 出错代码: /* 2 * ===== ...
- python字符串 常用函数 格式化字符串 字符串替换 制表符 换行符 删除空白 国际货币格式
# 字符串常用函数# 转大写print('bmw'.upper()) # BMW# 转小写print('BMW'.lower()) # bmw# 首字母大写print('how aae you ?'. ...
- 模板 - SG函数
https://scut.online/p/93 每次取走的石子是b的幂次.打表暴力发现规律. #include <bits/stdc++.h> using namespace std; ...