search
HomeWeb Front-endHTML TutorialV2EX论坛客户端之帖子信息爬取(一)_html/css_WEB-ITnose

前言

由于逛V2EX比较多,决定用闲暇时间做一个安卓客户端,开源在这里 https://github.com/ihgoo/V2EX

工欲善其事,必先利其器,整个项目以Gradle方式构建,Androidstudio开发。

公司的项目也转向AndroidStudio了,studio有个我特别喜欢的特性,输入变量的时候 记不住变量开头是怎么拼写的,能记住后面也会自动提示出来!还有就是插件多,开发向傻瓜化发展,只关注业务逻辑即可。

按照业务分模块

论坛客户端按照业务逻辑会分为以下模块

  • 在非登录状态下的浏览模块

    • 帖子列表浏览
    • 帖子详情浏览
  • 用户模块

    • 登录模块
    • 用户信息模块
  • 在登录状态下的模块

    • 带登录状态的回帖,帖子详情浏览
    • 收藏,点赞
    • 回帖提醒

依赖

依赖库会尽量考虑使用原生控件以及成熟的框架

compile 'com.jakewharton:butterknife:6.1.0'    compile 'com.squareup.retrofit:retrofit:1.9.0'compile 'com.squareup:otto:+'compile 'com.facebook.fresco:fresco:0.1.0+'compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'compile 'com.squareup.okhttp:okhttp:2.0.0'

butterknife:jack大神写的Ioc框架,媲美dagger,在idea/studio上面有支持butterknife的插件,一键findviewbyid!

retrofit: 强大的网络请求库。

fresco: 加载图片库,在使用这个之前,都是使用Imageloader的,刚出没多久的图片库,使用它是因为在项目中会有支持gif和支持图片渐进式显示的需求。

otto:eventBus框架!解耦神器,有了它,一切都变得简单了起来。

搭建项目,整理包结构

包结构如下图:

app:关于app的application等。

client:网络请求的报文头的定义,网络请求库的配置等。

core:基础框架,相当于mvc结构中的c,当然这里的c是指Controller

model:模型层

paser:解析层。无论是json,还是html,都是由此层解析生成实体类的。

persistence:放了一些常量类,数据库字段,Intnet请求字段,app配置字段等。

ui:视图展示层。

utils:一些顺手的工具类

项目以Gradle构建,app作为一个module,其他module作为挂载的形式挂到app上,优点是其他module可快速替换,且源码可修改(aar形式导入源码不可修改)。

关于API

由于调用官方json api有调用次数限制,于是考虑采用解析html页面来做。

电脑端html太过于庞大,为了省电降低app占用资源,解析的是wap端的页面,

可以通过修改请求头里的UA字段伪装成手机浏览器,在这里我用的是

public class ApiHeaders implements RequestInterceptor {    private String sessionId;    public void setSessionId(String sessionId) {        this.sessionId = sessionId;    }    public void clearSessionId() {        sessionId = null;    }    @Override public void intercept(RequestFacade request) {        request.addHeader("User-Agent", "Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19");        request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");        if (sessionId != null) {        }    }}

解析html,java里可以用一个叫做jsoup的库,媲美python中的pyquery,支持类Jqery选择器方式来抓取自己想要的资源,简单方便粗暴极了。唯一的缺点就是如果页面里有些元素是ajax形式形成的,那这个就抓瞎了,可以使用httpunit,不过httpunit存在性能问题,要启动一个浏览器内核来运行这个网页,网页上的js完成后,再抓取信息。

解析帖子列表

在帖子列表中需要关注解析这几个数据

  • avatar 作者头像
  • node 节点名称
  • title 标题
  • small fade (time) 发表时间
  • small fade author 作者名称
  • count_livid 回帖数以下是用jsoup解析的代码,解析成功后塞到ForumItemBean这个实体类中,以集合形式返回给listView的adapter中

    public class PaserFourmList {    public static ArrayList<ForumItemBean> paser2ForumItem(String string) {        Document document = Jsoup.parse(string);        Elements elements = document.select(".cell").select(".item");        ArrayList<ForumItemBean> list = new ArrayList<>();        for (Element element : elements) {            // avatar            // node            // title            // small fade (time)            // small fade author            // count_livid            String avatar = element.select(".avatar").first().attr("src");            String node = element.select(".node").first().html();            String username = element.select(".small > strong").first().text();            String countLivid = element.getElementsByClass("count_livid").text();            String time = element.select(".small").select(".fade").get(1).text();            String href = element.getElementsByClass("item_title").html();            if (href.length()!=0){                href = href.substring(12, href.indexOf("#"));            }            int indexOf = time.indexOf("前");            if (indexOf != -1) {                time = time.substring(0, indexOf);            }            ForumItemBean forumItemBean = new ForumItemBean();            Member member = new Member();            member.setAvatarMini(avatar);            member.setUsername(username);            forumItemBean.setId(Misc.parseInt(href, 0));            forumItemBean.setMember(member);            forumItemBean.setLastTime(time);            forumItemBean.setReplies(Misc.parseInt(countLivid, 0));            forumItemBean.setTitle(element.select(".item_title").first().select("[href]").html());            list.add(forumItemBean);        }        return list;    }}

使用Jsoup遇到的坑

在用jsoup的时候 像这种class带空格的,需要使用 element.select(“.content”).select(“.type”),才可以成功解析,使用element.select(“.content type”)是解析不出来的!

还有 # ,这种的,使用element.select(“.content-type”)也解析不出来,需要用element.getElementsByClass(“content-type”)才可以。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
HTML as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools