Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java to realize the artifact of chasing books

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to use Java to achieve the artifact of chasing books". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Jsoup is a Java HTML parser that can directly parse a URL address and HTML text content. It provides a very labor-saving API that can fetch and manipulate data through DOM,CSS and jQuery-like operations.

It's the same recipe as before, but this time with jsoup and javax.mail. Why send an email? That's because I want to check whether the novel on the website has been updated regularly, and if so, send me the latest chapter in time by e-mail, wouldn't it be better?

In this way, I can immediately know if the novel has been updated, and then if I update it, I can just go to my own mailbox and read it.

There must be no advertisements in the mailbox!

Public static HashMap cache = new HashMap (); public static Map cookies = new HashMap (); public static String url = https://www.biquwx.la/xxx/;

First, define several member variables, which are cache cache,cookie and url. Instead of using the database, I use a static HashMap for caching. Cookie is saved every time you crawl a web page, just try to simulate it like my browser. Finally, there is url, which is the actual address of the novel website, which is probably such a directory.

If there is an update, it will definitely appear on this page!

Here is the code I sent the request:

Connection con = Jsoup.connect (url) .userAgent ("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0") .header ("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") .header ("Accept-Language", "zh-CN,zh") Header ("Accept-Encoding", "gzip, deflate") .header ("Connection", "keep-alive") .header ("Upgrade-Insecure-Requests", "1") .cookies (cookies)

In order not to want him to send too many emails at once, I set up a separate starting chapter and put it in the txt text.

There is only one latest chapter title:

For the chapter that has been sent, I put it in the cache and will not repeat it:

/ / first check whether if (cache.containsKey (element.text () {System.out.println (element.text ()) + "has been sent, no need to repeat it!") ; continue;}

Finally, use Java to send an email to your mailbox.

MailAccount account = new MailAccount (); account.setHost ("smtp.163.com"); account.setPort (Integer.valueOf); account.setAuth (true); account.setSslEnable (Boolean.valueOf (true)); account.setConnectionTimeout (1000L); account.setSocketFactoryFallback (true); account.setFrom ("your sending mailbox"); account.setUser ("your sending mailbox"); account.setPass ("your sending mailbox authorization code") MailUtil.send (account, "xxxxxxxxx@qq.com", element.text (), content, true, new File [0]); System.out.println ("sent successfully!")

I use email box 163and send it to the corresponding QQ Mail.

Here are the emails I successfully received from the chapters of the novel:

With scheduled tasks, the effect is achieved, but this still has to be run on the server, otherwise it is impossible to keep the computer on all the time, .

The full source code is as follows:

/ * automatically send the latest chapter of the novel * / public static void sendMail () throws IOException {System.out.println ("start getting ready to crawl."); Connection con = Jsoup.connect (url) .userAgent ("Mozilla/5.0 (Windows NT 6.1; WOW64)" Rv:56.0) Gecko/20100101 Firefox/56.0 ") .header (" Accept "," text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 ") .header (" Accept-Language "," zh-CN,zh;q=0.8,en-US;q=0.5,en ") Header ("Accept-Encoding", "gzip, deflate") .header ("Connection", "keep-alive") .header ("Upgrade-Insecure-Requests", "1") .cookies (cookies); Response execute = con.execute (); cookies = execute.cookies () String body = execute.body () .toString (); Document doc = Jsoup.parse (body); Elements as = doc.getElementsByTag ("a"); boolean shouldSend = false String start = FileUtil.readString ("C:/start.txt", "UTF-8"); / / read the start section. If the service is restarted, you must modify the file for (Iterator iterator = as.iterator (); iterator.hasNext ();) {Element element = (Element) iterator.next () If (element.text () .equals (start)) {shouldSend = true } if (shouldSend & & element.text (). StartsWith ("1st")) {/ / first check whether if (cache.containsKey (element.text () { System.out.println (element.text () + "has been sent There is no need to repeat sending! ") ; continue;} con = Jsoup.connect (url + element.attr ("href")); String content = Jsoup.parse (con.execute (). Body ()) .getElementById ("content") .html () Cache.put (element.text (), ""); MailAccount account = new MailAccount (); account.setHost ("smtp.163.com") Account.setPort (Integer.valueOf); account.setAuth (true); account.setSslEnable (Boolean.valueOf (true)); account.setConnectionTimeout (1000L); account.setSocketFactoryFallback (true) Account.setFrom ("your sending mailbox"); account.setUser ("your sending mailbox"); account.setPass ("authorization code"); / / MailUtil.send (account, "your receiving mailbox", element.text (), content, true, new File [0]) System.out.println ("sent successfully!") This is the end of the content of "how to use Java to realize the artifact of chasing books". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report