# -*- coding: utf-8 -*-
import requests,os
from lxml import etree
from pymongo import *
class Boke(object):
def __init__(self):
self.url ="https://www.cnblogs.com/cate/python/"
self.headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.90 Safari/537.36 2345Explorer/9.3.2.17331'}
def get_data(self,url):
response = requests.get(url,headers=self.headers)
return response.content
def xml_data(self,data):
html = etree.HTML(data)
mes = html.xpath("//div[@class='post_item']")
for i in mes:
dict={}
info_url = i.xpath("./div[@class='post_item_body']/h3/a/@href")[0]
self.info_data(info_url)
dict['url'] = info_url
self.write_dbs(dict)
def info_data(self,data):
path = "f:/woc/"
if not os.path.exists(path):
os.makedirs(path)
mes = self.get_data(data)
html = etree.HTML(mes)
list = html.xpath("//div[@id='topics']/div[@class='post']")
# print(list)
for x in list:
dictlist = {}
title = x.xpath("./h1[@class='postTitle']/a/text()")[0]
info = x.xpath("./div[@class='postBody']//text()")
dictlist['title'] = title
dictlist['info'] = info
self.write1_dbs(dictlist)
def dbs(self):
connect = MongoClient('127.0.0.1',27017)
conn = connect['boke']
conn1 =conn['zhu']
conn2 =conn['info']
return conn1,conn2
def write_dbs(self,data):
conn1,conn2 = self.dbs()
conn1.insert_one(data)
result=conn1.find()
for i in result:
print(i)
def write1_dbs(self,data):
conn1, conn2 = self.dbs()
conn2.insert_one(data)
result = conn2.find()
for i in result:
print(i)
def run(self):
url = self.url
data = self.get_data(url)
self.xml_data(data)
if __name__ == '__main__':
boke = Boke()
boke.run()

bokeyuan_python文章爬去入mongodb读取--LOWBIPROGRAMMER的更多相关文章

  1. 360电影主页和详情页爬去入Mysql库链表读取--lowbiprogrammer

    import requests,os,jsonfrom lxml import etreefrom pymysql import *class Movie(object):def __init__(s ...

  2. Selenium爬取淘宝商品概要入mongodb

    准备: 1.安装Selenium:终端输入 pip install selenium 2.安装下载Chromedriver:解压后放在…\Google\Chrome\Application\:如果是M ...

  3. java批量爬去电影资源

    摘要 网上有很多个人站来分享电影资源,其实有时候我们自己也想做这个一个电影站来分享资源.但是这个时候就有一个问题,电影的资源应该从哪里来呢?难道要自己一条条手动去从网络上获取,这样无疑是缓慢而又效率低 ...

  4. python unicode 转中文 遇到的问题 爬去网页中遇到编码的问题

    How do convert unicode escape sequences to unicode characters in a python string 爬去网页中遇到编码的问题 Python ...

  5. 利用python的爬虫技术爬去糗事百科的段子

    初次学习爬虫技术,在知乎上看了如何爬去糗事百科的段子,于是打算自己也做一个. 实现目标:1,爬取到糗事百科的段子 2,实现每次爬去一个段子,每按一次回车爬取到下一页 技术实现:基于python的实现, ...

  6. python爬去电影天堂恐怖片+游戏

    1.爬去方式python+selenium 2.工作流程 selenium自动输入,自动爬取,建立文件夹,存入磁力链接到记事本 3.贴上代码 #!/usr/bin/Python# -*- coding ...

  7. 破解微信防盗链&微信公众号文章爬取方案

    破解微信图文防盗链:https://www.cnblogs.com/xsxshmily/p/8000043.html 图片解除防盗链:https://blog.csdn.net/show_ljw/ar ...

  8. Java基础-爬虫实战之爬去校花网网站内容

    Java基础-爬虫实战之爬去校花网网站内容 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 爬虫这个实现点我压根就没有把它当做重点,也没打算做网络爬虫工程师,说起爬虫我更喜欢用Pyt ...

  9. Python学习之路 (五)爬虫(四)正则表示式爬去名言网

    爬虫的四个主要步骤 明确目标 (要知道你准备在哪个范围或者网站去搜索) 爬 (将所有的网站的内容全部爬下来) 取 (去掉对我们没用处的数据) 处理数据(按照我们想要的方式存储和使用) 什么是正则表达式 ...

随机推荐

  1. mysql相关攻击代码收集

    1.批处理文件内容 @echo off net user li /add net user li /active:yes net localgroup Administrators li /add 2 ...

  2. requests 安装

    requests 是用来发送 HTTP 请求的一个库,requests 是对 urllib 和 urllib2 进行封装的一个模块,用来取代 urllib 和 urllib2,可以使用以下两种方法安装 ...

  3. windows下nodejs与coffeeScript环境搭建

    [本文档摘抄自网上资料] 安装NodeJS和CoffeeScript方法 首先安装Node(因为nodeJs是服务器端javascript运行环境),到http://nodejs.org/下载对应格式 ...

  4. PHP一致性哈希实现。。

    <?php /** *@author:xiaojiang 20140222 * 一致性哈希php 实现 */ class MyHash{ //虚拟节点数 private $_virtualCou ...

  5. c# MVC Take的使用

    Take的使用 myPicture = dbContext.MyPictures.Where(u => u.Width == request.Width && u.Height ...

  6. TCP端口号范围及分类

    https://blog.csdn.net/my_heart_/article/details/52601924 端口号的范围是从1-65535 端口的概念:  在网络技术中,端口(Port)大致有两 ...

  7. JS-缓冲运动基础结构

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. Android 查看system/bin目录下支持哪些命令?

    C:\Users\yonghuming>adb shell "ls system/bin" >log acpiadbdamapp_processapp_process3 ...

  9. Android.mk(4) 依赖:目标编程的模式

    https://www.jianshu.com/p/3777a585a8d0 另一种范式 我一直觉得,Makefile确实是C/C++程序员的良配,因为Makefile所使用的两种范式都是C/C++程 ...

  10. Mac下一款门罗币挖矿木马的简要分析

    背景 最近在应急中发现了一款Mac上的挖矿木马,目标是挖门罗币,经过走访,受害用户都有从苹果电脑上安装第三方dmg的经历(其中可以确定一款LOL Mac私服安装app会导致该木马),怀疑在网上很多第三 ...