python爬虫 赶集网
#coding=utf-8
import requests
from lxml import etree
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, String, Integer
from sqlalchemy.orm import sessionmaker def requests_view(response):
import webbrowser
requests_url = response.url
base_url = '<head><base href="%s">' %(requests_url)
base_url = base_url.encode('utf-8')
content = response.content.replace(b"<head>",base_url)
tem_html = open('tmp.html','wb')
tem_html.write(content)
tem_html.close()
webbrowser.open_new_tab("tmp.html") host = "http://sz.ganji.com/fang1/o{}"
max = 10 engine = create_engine('mysql+mysqldb://root:root@192.168.33.30:3306/python?charset=utf8',echo=True,encoding='utf8')
Base = declarative_base() class Ganji(Base): __tablename__ = 'ganji' id = Column(Integer, primary_key=True)
title = Column(String(100))
money = Column(String(100))
info = Column(String(100))
create_time = Column(String(30)) def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, self.username)
# Base.metadata.create_all(engine)
# exit()
def save_data(title,money,info):
# 创建session对象:
DBSession = sessionmaker(bind=engine)
session = DBSession()
# 创建新User对象:
import datetime
create_time = datetime.datetime.now()
new_ganji = Ganji( title=title,money=money,info=info,create_time="test")
# 添加到session:
session.add(new_ganji)
# 提交即保存到数据库:
session.commit()
# 关闭session:
session.close() def get_html(url):
headers = {'Referer':'http://callback.ganji.com/firewall/valid/1902788594.do?namespace=ganji_zufang_list_pc&url=http%3A%2F%2Fsz.ganji.com%2Ffang1%2F','User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36'}
response = requests.get(url,headers=headers)
if response.status_code == 200:
#requests_view(response)
#strip
html = etree.HTML(response.content.decode('utf-8'))
items = html.xpath(".//div[@class='f-main-list']/div/div")
print(len(items))
for i in items:
title = i.xpath(".//dd[@class='dd-item title']/a/text()")
money = i.xpath(".//dd[@class='dd-item info']/div[@class='price']/span/text()")
info = i.xpath(".//dd[@class='dd-item size']/span/text()")
print(info)
title = ' '.join(title)
money = ' '.join(money)
info = ' '.join(info)
if len(title) > 0 and len(money) >0 and len(info) > 0 :
save_data(title,money,info)
else:
print("未获取到数据"); else:
print("请求失败")
try:
for i in range(1,max):
url = host.format(i)
print(url)
get_html(url)
except Exception as e:
print(str(e))
python爬虫 赶集网的更多相关文章
- python爬虫实例--网易云音乐排行榜爬虫
网易云音乐,以前是有个api 链接的json下载的,现在没了, 只有音乐id,title , 只能看播放请求了, 但是播放请求都是加密的值,好坑... 进过各种努力, 终于找到了个大神写的博客,3.6 ...
- Python 爬虫 当当网图书 scrapy
目标站点需求分析 获取当当网每个图书名字和评论数 涉及的库 scrapy,mysql 获取解析单页源码 保存到数据库中 结果
- 使用scrapy框架做赶集网爬虫
使用scrapy框架做赶集网爬虫 一.安装 首先scrapy的安装之前需要安装这个模块:wheel.lxml.Twisted.pywin32,最后在安装scrapy pip install wheel ...
- Python爬虫之爬取慕课网课程评分
BS是什么? BeautifulSoup是一个基于标签的文本解析工具.可以根据标签提取想要的内容,很适合处理html和xml这类语言文本.如果你希望了解更多关于BS的介绍和用法,请看Beautiful ...
- Python爬虫开源项目代码,爬取微信、淘宝、豆瓣、知乎、新浪微博、QQ、去哪网等 代码整理
作者:SFLYQ 今天为大家整理了32个Python爬虫项目.整理的原因是,爬虫入门简单快速,也非常适合新入门的小伙伴培养信心.所有链接指向GitHub,祝大家玩的愉快 1.WechatSogou [ ...
- python链家网高并发异步爬虫asyncio+aiohttp+aiomysql异步存入数据
python链家网二手房异步IO爬虫,使用asyncio.aiohttp和aiomysql 很多小伙伴初学python时都会学习到爬虫,刚入门时会使用requests.urllib这些同步的库进行单线 ...
- python爬虫:爬取慕课网视频
前段时间安装了一个慕课网app,发现不用注册就可以在线看其中的视频,就有了想爬取其中的视频,用来在电脑上学习.决定花两天时间用学了一段时间的python做一做.(我的新书<Python爬虫开发与 ...
- 用Python爬虫爬取广州大学教务系统的成绩(内网访问)
用Python爬虫爬取广州大学教务系统的成绩(内网访问) 在进行爬取前,首先要了解: 1.什么是CSS选择器? 每一条css样式定义由两部分组成,形式如下: [code] 选择器{样式} [/code ...
- Python爬虫教程-15-读取cookie(人人网)和SSL(12306官网)
Python爬虫教程-15-爬虫读取cookie(人人网)和SSL(12306官网) 上一篇写道关于存储cookie文件,本篇介绍怎样读取cookie文件 cookie的读取 案例v16ssl文件:h ...
随机推荐
- Python2和Python3
1. 字符编码 1.1. Python2默认为ACSII编码 1.2. Python3为Unicode 2. Unicode和UTF8和GBK编码的关系 utf8:中文3字节 ...
- mvc:interceptor拦截器使用
在spring-mvc.xml里面配置标签 <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/* ...
- C语言,初次见面~
C语言是一门介于低级语言(如汇编语言)和高级语言(如Java,Python)之间的一门编程语言,所以它兼有两类语言的一些优点,并且具有自身的一些特点. 1.c语言的高效性.c语言具有通常是汇编语言才具 ...
- 闰年相关的问题v3.0——计算有多少闰年
# include<stdio.h>int main(){ int a,b,i; int sum = 0; printf("Input your birth year:" ...
- python 感叹号的作用
1. !表示反转逻辑表达式的值 2. 打印格式控制中: x!r代表repr(x),x!s代表str(x),x!a代表ascii(x)
- java中reader和writer部分的笔记
输入和输出流:获取流对象从文件中获取InputStream in = Files.newInputStream(path);OutputStream out = Files.newOutputStre ...
- 【转】Odoo开发之:工作流 workflow
在OpenERP中,工作流是管理一组“所做的事情”(与一些数据模型的记录关联)的人为现象.工作流提供了高级别的方式来组织记录要上做的事情. 具体地说,工作流是一个定向的路径,这里节点称为活动并且弧线称 ...
- 北京Uber优步司机奖励政策(12月9日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Vue框架核心之数据劫持
本文来自网易云社区. 前瞻 当前前端界空前繁荣,各种框架横空出世,包括各类mvvm框架横行霸道,比如Angular.Regular.Vue.React等等,它们最大的优点就是可以实现数据绑定,再也不需 ...
- Elastic stack ——X-Pack安装
X-Pack是一个Elastic Stack的扩展,将安全,警报,监视,报告和图形功能包含在一个易于安装的软件包中.在Elasticsearch 5.0.0之前,您必须安装单独的Shield,Watc ...