python爬虫简单实现,并在java中调用python脚本,将数据保存在json文件中
# coding:utf-8 import urllib2
from bs4 import BeautifulSoup
import json
import sys reload(sys) sys.setdefaultencoding('utf-8')
class dataBean(object) : def __init__(self, title, url,date):
self.date = date
self.url = url
self.title = title
def obj_2_json(obj):
return {
"title":obj.title,
"url":obj.url,
"date":obj.date
}
url = "http://localhost:8088/news.html"
response3 = urllib2.urlopen(url)
soup = BeautifulSoup(response3.read(), 'html.parser', from_encoding='utf-8')
links = soup.find_all('a',class_='')
data=[]
contents = soup.find('ul', class_="w_newslistpage_list").findAll("li") for content in contents:
bean = dataBean(content.find("span").find("a").get_text(), content.find("span").find("a")['href'],
content.find('span', class_="date").get_text())
data.append(dataBean(content.find("span").find("a").get_text(), content.find("span").find("a")['href'],
content.find('span', class_="date").get_text())) jsondata= json.dumps(data,default=dataBean.obj_2_json, ensure_ascii=False,encoding='utf-8')
fileObject = open('data.json', 'w')
fileObject.write(jsondata)
fileObject.close()
print jsondata
java中调用,借助jython.jar,并将bs4文件拷贝在当前文件夹下即可
import org.python.core.Py;
import org.python.core.PyString;
import org.python.util.PythonInterpreter; public class Main {
//jython安装
public static void main(String[] args) {
String code = "# -*- coding: utf-8 -*-\n" +
"import sys\n" +
"reload(sys)\n" + "import urllib2\n" +
"sys.setdefaultencoding('utf-8')\n" +
"import json\n";
new Thread(new Runnable() {
@Override
public void run() {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("from bs4 import BeautifulSoup");
PyString code2 = Py.newStringUTF8(code);
interpreter.exec(code2);
interpreter.execfile("D:\\java\\test\\src\\GetNewsDataToLocal.py"); }
}
).start();
} }
可在当前文件夹看到json文件
python爬虫简单实现,并在java中调用python脚本,将数据保存在json文件中的更多相关文章
- 第三天,爬取伯乐在线文章代码,编写items.py,保存数据到本地json文件中
一. 爬取http://blog.jobbole.com/all-posts/中的所有文章 1. 编写jobbole.py简单代码 import scrapy from scrapy. ...
- 直接把数据库中的数据保存在CSV文件中
今天突然去聊就来写一个小小的demo喽,嘿嘿 public partial class Form1 : Form { public Form1() { InitializeComponent(); } ...
- 使用scrapy爬取的数据保存到CSV文件中,不使用命令
pipelines.py文件中 import codecs import csv # 保存到CSV文件中 class CsvPipeline(object): def __init__(self): ...
- 使用jsp读取某个目录下的所有文件名,并保存在json文件中
<%@page import="java.io.File"%> <%@page import="java.io.FileWriter"%> ...
- Sql数据保存到Excel文件中
public string ExportExcel( DataSet ds,string saveFileName) { try { if (ds == null) return "数据库为 ...
- c# 将datatable中的数据保存到excel文件中
using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Lin ...
- 如何在Java中调用Python代码
有时候,我们会碰到这样的问题:与A同学合作写代码,A同学只会写Python,而不会Java, 而你只会写Java并不擅长Python,并且发现难以用Java来重写对方的代码,这时,就不得不想方设法“调 ...
- 在Java中调用Python
写在前面 在微服务架构大行其道的今天,对于将程序进行嵌套调用的做法其实并不可取,甚至显得有些愚蠢.当然,之所以要面对这个问题,或许是因为一些历史原因,或者仅仅是为了简单.恰好我在项目中就遇到了这个问题 ...
- 在Java中调用Python代码
极少数时候,我们会碰到类似这样的问题:与A同学合作写代码, A同学只会写Python,不熟悉Java ,而你只会写Java不擅长Python,并且发现难以用Java来重写对方的代码,这时,就不得不想方 ...
随机推荐
- 自定义flask转换器
自定义flask转换器 以匹配手机号为例: # 1. 定义自己的转换器 class MobileConverter(BaseConverter): def __init__(self, url_map ...
- Jupyter notebook部署引导
一.简介方面很多博客写得比较好,主要转发几篇: 1.对Jupyter notebook 的整体进行介绍: https://www.itcodemonkey.com/article/6025.html ...
- java之spring
Spring Spring中的基本概念1.IOC/DI对象的属性由自己创建,为正向流程,而由Spring创建,为控制反转.DI(依赖注入)为实现IOC的一种方式,通过配置文件或注解包含的依赖关系创建与 ...
- 2018年5月20日--西安icpc邀请赛打铁总结
2018年5月20日--西安icpc邀请赛打铁总结 事后诸葛亮 大致回顾一下比赛,29号的热身赛和30号的正式赛. 热身赛总共三道题,一个小时,没有AC一道题目. A题是一个几何题目,审题时犯了一个 ...
- 软件测试第二周个人作业:WordCount
github地址:https:/github.com/muzhailong/wc.git 第一次写博客很不容易,也算是一个好的开始吧. 1. 个人作业要求 作业简述:根据WordCount的需求描 ...
- Oracle 后台进程(六)PMON进程
一.PMON简介 二.PMON的工作内容如下: 1.监控后台进程运行状况 2.如果某些进程异常中断,PMON去释放会话资源以及占用的锁LOCK 3.更新事务表的标志以及清除事务XID的标记 4.清除异 ...
- OSI七层协议模型
OSI七层模型详解 TCP/IP协议 链接:https://www.nowcoder.com/questionTerminal/b2ccf60bbb13483b94b4bffe200b4f3c 来源: ...
- Codeforces 1221 E Game With String
题面 第一眼以为是SG函数找规律题,然后发现并不是公平游戏.... 不过后来想了想,其实这样反而更好做. 这个游戏的一个显然的特性是,任何时候当场上存在长度 ∈[b,a)的块时,Bob必胜.(考虑贪心 ...
- House Lawn Kattis - houselawn
Problem You have just bought a new house, and it has a huge, beautiful lawn. A lawn that needs cutti ...
- ansible的become
# ansible sudo 问题 官方下载centos7.6fcow2镜像不给直接远程ssh了,所以必须sudo,但是有的命令sudo也解决不了的如管道重定向还有多个命令组合. 解决办法: vim ...