qq 国内新闻
http://news.qq.com/c/816guonei_1.htm
http://news.qq.com/c/816guonei_2.htm
........
<a target="_blank" class="pic" href="/a/20160430/004728.htm"> <img class="picto" src="http://img1.gtimg.com/news/pics/hv1/130/103/2061/134042920_small.jpg"></a> <em class="f14 l24"> <a target="_blank" class="linkto" href="/a/20160430/004728.htm"> 学校老师私开小卖部 带方便面进校遭食堂老板殴打 </a> </em> <p class="l22"> 达州市渠县三汇中学王道明老师的妻子邓女士,在谈到丈夫与学校食堂承包商刘志勇,因携带方便面进校门发生纠纷继而被刘志勇打一事时,双眼恍惚,充满了恐惧。 </p> <a target=\"_blank\" class=\"pic\" href=\"([^\"]*)\"> <img class=\"picto\" src=\"([^\"]*)\"> </a> <em class=\"f14 l24\"> <a target=\"_blank\" class=\"linkto\" href=\"[^\"]*\"> ([^</a>]*) </a> </em> <p class=\"l22\"> ([^</p>]*) </p>
demo 如图:
根据返回内容 进行正则匹配,获取 需要布局信息 QQNewsItemEntity
(图片展示不好,可以查看图片附件)
package com.curiousby.fitnessandappointment.utils; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.curiousby.fitnessandappointment.entity.QQNewsItemEntity; public class QQNewsMatcherPerformer { public static List<QQNewsItemEntity> parseHtmlData(String result) { List<QQNewsItemEntity> list = new ArrayList<QQNewsItemEntity>(); Pattern pattern = Pattern .compile("<a target=\"_blank\" class=\"pic\" href=\"([^\"]*)\"><img class=\"picto\" src=\"([^\"]*)\"></a><em class=\"f14 l24\"><a target=\"_blank\" class=\"linkto\" href=\"[^\"]*\">([^</a>]*)</a></em><p class=\"l22\">([^</p>]*)</p>"); Matcher matcher = pattern.matcher(result); while (matcher.find()) { QQNewsItemEntity model = new QQNewsItemEntity(); model.setNewsDetailUrl(matcher.group(1).trim()); model.setUrlImgAddress(matcher.group(2).trim()); model.setNewsTitle(matcher.group(3).trim()); model.setNewsSummary(matcher.group(4).trim()); list.add(model); } return list; } }
package com.curiousby.fitnessandappointment.entity; public class QQNewsItemEntity { //新闻详情地址 private String newsDetailUrl; //新闻图片地址 private String urlImgAddress; //新闻标题 private String newsTitle; //新闻概要 private String newsSummary; public String getUrlImgAddress() { return urlImgAddress; } public void setUrlImgAddress(String urlImgAddress) { this.urlImgAddress = urlImgAddress; } public String getNewsDetailUrl() { return newsDetailUrl; } public void setNewsDetailUrl(String newsDetailUrl) { this.newsDetailUrl = newsDetailUrl; } public String getNewsTitle() { return newsTitle; } public void setNewsTitle(String newsTitle) { this.newsTitle = newsTitle; } public String getNewsSummary() { return newsSummary; } public void setNewsSummary(String newsSummary) { this.newsSummary = newsSummary; } }
package com.curiousby.fitnessandappointment.request.http; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.json.JSONObject; import android.util.Log; import com.android.volley.Request.Method; import com.android.volley.NetworkResponse; import com.android.volley.Response; import com.android.volley.Response.ErrorListener; import com.android.volley.Response.Listener; import com.android.volley.VolleyError; import com.android.volley.toolbox.HttpHeaderParser; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import com.curiousby.fitnessandappointment.MyApplication; import com.curiousby.fitnessandappointment.constants.Constants; import com.curiousby.fitnessandappointment.entity.FeedAllEntity; import com.curiousby.fitnessandappointment.entity.FeedCommentEntity; import com.curiousby.fitnessandappointment.entity.FeedZanEntity; import com.curiousby.fitnessandappointment.entity.QueryResultJson; import com.curiousby.fitnessandappointment.quote.volley.GsonRequest; import com.curiousby.fitnessandappointment.request.dbmanager.QQNewsHttpManager; import com.curiousby.fitnessandappointment.request.dbmanager.TrendHttpManager; import com.curiousby.fitnessandappointment.utils.JSONUtil; import com.curiousby.fitnessandappointment.utils.JsonParser; import com.google.gson.JsonElement; public class QQNewsHttpRequest { public static void getQQNewsDate(final int page){ String url = Constants.getQQNewsUrl(page); StringRequest request = new StringRequest(url, new Listener<String>() { @Override public void onResponse(String response) { if (page ==1 ) QQNewsHttpManager.getQQNewsByPageFirst(response); else QQNewsHttpManager.getQQNewsByPage(response); } }, new ErrorListener() { @Override public void onErrorResponse(VolleyError error) { QQNewsHttpManager.getQQNewsByPageError(); } }) /* { @Override protected Response<String> parseNetworkResponse( NetworkResponse response) { String str = null; try { str = new String(response.data,"utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return Response.success(str, HttpHeaderParser.parseCacheHeaders(response)); } }*/ ; Volley.newRequestQueue(MyApplication.newInstance()).add(request); } }
package com.curiousby.fitnessandappointment.activity; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import com.curiousby.fitnessandappointment.MyApplication; import com.curiousby.fitnessandappointment.R; import com.curiousby.fitnessandappointment.adpter.QQNewsAdapter; import com.curiousby.fitnessandappointment.constants.Constants; import com.curiousby.fitnessandappointment.entity.QQNewsItemEntity; import com.curiousby.fitnessandappointment.quote.xlistview.MsgListView; import com.curiousby.fitnessandappointment.quote.xlistview.MsgListView.IXListViewListener; import com.curiousby.fitnessandappointment.request.event.QQNewsStringHttpEvent; import com.curiousby.fitnessandappointment.request.event.base.RequestEvent; import com.curiousby.fitnessandappointment.request.http.QQNewsHttpRequest; import com.curiousby.fitnessandappointment.utils.QQNewsMatcherPerformer; import de.greenrobot.event.EventBus; public class QQNewsActivity extends BaseActivity implements IXListViewListener ,OnItemClickListener{ private MsgListView mListView; private List<QQNewsItemEntity> mDataList; private QQNewsAdapter mAdapter; private static int start = 1; private Context mContext; public final static String QQ_NEWS_DETAIL_URL = "qq_news_detail_url"; @Override protected void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.qqnews_list); this.mContext = QQNewsActivity.this; initUtils(); initView( ); initListeners(); EventBus.getDefault().register( this ); start =1; QQNewsHttpRequest.getQQNewsDate(start); } private void initUtils() { mDataList = new ArrayList<QQNewsItemEntity>(); mAdapter = new QQNewsAdapter(mContext); mAdapter.setmDataList(mDataList); } private void initView() { mListView = (MsgListView) this.findViewById(R.id.qq_news_list); mListView.setAdapter(mAdapter); } private void initListeners() { mListView.setPullLoadEnable(true); mListView.setPullRefreshEnable(true); mListView.setXListViewListener(this); mListView.setAdapter(mAdapter); mListView.setOnItemClickListener( this ); } @Override public void onRefresh() { start =1; mListView.setPullLoadEnable(true); mListView.setPullRefreshEnable(true); QQNewsHttpRequest.getQQNewsDate(start); } @Override public void onLoadMore() { start += 1; QQNewsHttpRequest.getQQNewsDate(start); } @Override public void onDestroy() { EventBus.getDefault().unregister( this ); super.onDestroy(); } public void onEventMainThread(RequestEvent requestEvent){ if(requestEvent instanceof QQNewsStringHttpEvent){ QQNewsStringHttpEvent event = (QQNewsStringHttpEvent) requestEvent; switch(event.status){ case HTTP_ERROR: mListView.stopRefresh(); mListView.stopLoadMore(); mListView.setPullLoadEnable(false); break; case HTTP_START: { mListView.stopRefresh(); mListView.stopLoadMore(); String result = event.data; mAdapter.clearMDataList(); List<QQNewsItemEntity> list = QQNewsMatcherPerformer.parseHtmlData(result); mAdapter.setmDataList(list); mAdapter.notifyDataSetChanged(); } break; case HTTP_SUCCESS: { mListView.stopRefresh(); mListView.stopLoadMore(); String result = event.data; List<QQNewsItemEntity> list = QQNewsMatcherPerformer.parseHtmlData(result); mAdapter.addMDataList(list); mAdapter.notifyDataSetChanged(); } break; default: break; } } } @Override public void onItemClick( AdapterView<?> parent, View view, int position, long id) { QQNewsItemEntity item = mAdapter.getItem(position - 1 ); if (item != null) { Intent msgIntent = new Intent(); Log.e("baoyou", Constants.url_qq_news_base + item.getNewsDetailUrl()); Bundle bundle = new Bundle(); // Bundle的底层是一个HashMap<String, Object bundle.putString(QQNewsActivity.QQ_NEWS_DETAIL_URL, item.getNewsDetailUrl() ); msgIntent.putExtra("bundle", bundle); msgIntent.setClass(MyApplication.newInstance(), QQNewsDetailActivity.class); startActivityForResult( msgIntent, 1000 ); } } }
package com.curiousby.fitnessandappointment.activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.webkit.WebSettings; import android.webkit.WebSettings.LayoutAlgorithm; import android.webkit.WebView; import android.webkit.WebViewClient; import com.curiousby.fitnessandappointment.R; import com.curiousby.fitnessandappointment.constants.Constants; public class QQNewsDetailActivity extends BaseActivity { private WebView webView; private String url ="https://www.baidu.com"; @Override protected void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.qqnews_list_item_detail); initUtils(); initView( ); initListeners(); } private void initListeners() { webView.loadUrl(url); showWaitingDialog(""); webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); dismissWaitingDialog(); return true; } }); } private void initView() { webView = (WebView) this.findViewById(R.id.wv_qq_news_item_detail_webview); WebSettings settings = webView.getSettings(); settings.setJavaScriptEnabled(true); //支持js //settings.setPluginsEnabled(true); //支持插件 settings.setUseWideViewPort(false); //将图片调整到适合webview的大小 settings.setSupportZoom(true); //支持缩放 settings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); } private void initUtils() { Intent intent = getIntent(); Bundle bundle = intent.getBundleExtra("bundle"); String path =bundle.getString(QQNewsActivity.QQ_NEWS_DETAIL_URL); url = Constants.url_qq_news_base + path; } }
package com.curiousby.fitnessandappointment.adpter; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.curiousby.fitnessandappointment.R; import com.curiousby.fitnessandappointment.entity.QQNewsItemEntity; import com.curiousby.fitnessandappointment.quote.volley.ImageCacheManager; public class QQNewsAdapter extends BaseAdapter{ private LayoutInflater mInflater; private List<QQNewsItemEntity> mDataList; private Context mContext; public QQNewsAdapter(Context mContext) { this.mContext = mContext; this.mInflater = LayoutInflater.from(mContext); } public List<QQNewsItemEntity> getmDataList() { return mDataList; } public void setmDataList(List<QQNewsItemEntity> mDataList) { if (this.mDataList == null ) { this.mDataList = new ArrayList<QQNewsItemEntity>(); } this.mDataList = mDataList; } public void addMDataList(List<QQNewsItemEntity> mDataList) { this.mDataList.addAll(mDataList); } public void clearMDataList( ) { this.mDataList.clear(); } @Override public int getCount() { if (mDataList != null && mDataList.size() > 0) return mDataList.size(); return 0; } @Override public QQNewsItemEntity getItem(int position) { if (mDataList != null && mDataList.size() > 0) return mDataList.get(position); return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if ( convertView == null || convertView.getTag() ==null ) { convertView = mInflater.inflate( R.layout.qqnews_list_item,parent, false); viewHolder = new ViewHolder(convertView); convertView.setTag( viewHolder ); }else{ viewHolder = ( ViewHolder ) convertView.getTag(); } QQNewsItemEntity qqnie = getItem(position); if (qqnie != null) { ImageCacheManager.loadImage(qqnie.getUrlImgAddress(),viewHolder.qqnewspic, getBitmapFromRes(R.drawable.ic_launcher), getBitmapFromRes(R.drawable.ic_launcher)); viewHolder.qqnewstitle.setText(qqnie.getNewsTitle()); viewHolder.qqnewscontent.setText(qqnie.getNewsSummary()); } return convertView; } class ViewHolder{ public ImageView qqnewspic; public TextView qqnewstitle; public TextView qqnewscontent; public ViewHolder(View baseView) { this.qqnewspic = (ImageView) baseView.findViewById(R.id.qq_news_image_link); this.qqnewstitle = (TextView) baseView.findViewById(R.id.qq_news_txt_title); this.qqnewscontent = (TextView) baseView.findViewById(R.id.qq_news_txt_summary); } } public Bitmap getBitmapFromRes(int resId) { Resources res = mContext.getResources(); return BitmapFactory.decodeResource(res, resId); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="1dp"> <ImageView android:id="@+id/qq_news_image_link" android:layout_width="80dp" android:layout_height="wrap_content" android:scaleType="centerCrop" android:layout_centerVertical="true" android:layout_above="@+id/v_qq_news_line" android:background="@drawable/ic_launcher" /> <LinearLayout android:layout_marginLeft="10dp" android:id="@+id/ll_qq_news_contentandhead" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/qq_news_image_link" android:orientation="vertical"> <TextView android:id="@+id/qq_news_txt_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="标题" android:textSize="16dp" /> <TextView android:layout_marginTop="5dp" android:id="@+id/qq_news_txt_summary" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="内容" android:layout_marginBottom="3dp" android:textSize="12dp" /> </LinearLayout> <View android:id="@+id/v_qq_news_line" android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginBottom="3dp" android:layout_marginTop="5dp" android:background="@color/grey" android:layout_alignParentBottom="true"/> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RelativeLayout android:id="@+id/rl_qq_news_head" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/title_bar" android:gravity="center_vertical" android:orientation="horizontal" android:padding="3dp" android:layout_alignParentTop="true" > <TextView android:id="@+id/tv_qq_news_head_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="新闻" android:textColor="#FFF" android:textSize="20sp" android:textStyle="bold" /> </RelativeLayout> <com.curiousby.fitnessandappointment.quote.xlistview.MsgListView android:id="@+id/qq_news_list" android:layout_below="@id/rl_qq_news_head" android:background="@color/white" android:layout_width="match_parent" android:layout_height="fill_parent" android:divider="@null"> </com.curiousby.fitnessandappointment.quote.xlistview.MsgListView> </RelativeLayout>
package com.curiousby.fitnessandappointment.constants; import java.util.ArrayList; import java.util.List; import com.curiousby.fitnessandappointment.R; import com.curiousby.fitnessandappointment.entity.GridEntity; public class Constants { public final static String BASEIP ="http://127.0.0.1:8080"; public final static String login_url = BASEIP + "/RepositoryPic/WebService/rest/user/login"; public final static String friendship_url = BASEIP + "/RepositoryPic/WebService/rest/friendShip/friendShips/"; public final static String url_addDeviceToTag = BASEIP + "/RepositoryPic/WebService/rest/pushService/addDevicesToTag"; public final static String url_post_sendMessageToTag =BASEIP + "/RepositoryPic/WebService/rest/pushService/sendMessageToTag"; public final static String url_feed =BASEIP + "/RepositoryPic/WebService/rest/feedAll/feedAlls/user"; public final static String url_add_feed_comment =BASEIP + "/RepositoryPic/WebService/rest/feedAll/feedCommemtAdd"; public final static String url_add_feed_zan =BASEIP + "/RepositoryPic/WebService/rest/feedAll/feedZanAdd"; public final static String url_feed_create = BASEIP + "/RepositoryPic/WebService/rest/feedAll/createFeed"; public final static String url_qq_news_base="http://news.qq.com"; public static String url_qq_news_guonei = url_qq_news_base +"/c/816guonei_?.htm"; public static String getQQNewsUrl (int page){ return url_qq_news_guonei.replace("?", page+""); } public static List<GridEntity> getGridItem() { List<GridEntity> list = new ArrayList<GridEntity>(); list.add(new GridEntity(1, "动态", R.drawable.mian_trend_grid_trend, 1)); list.add(new GridEntity(2, "悦动圈", R.drawable.mian_trend_grid_yuedongquan, 2)); list.add(new GridEntity(3, "新闻", R.drawable.qq_ketang, 3)); return list; } }
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!
相关推荐
这个正则表达式可以用来清除 HTML 文档中的标签,从而提取纯文本内容。`<(\S*?)[^>]*>.*?</\1>` 匹配带有闭合标签的 HTML 元素,而 `*?/>` 则匹配自闭合标签。 #### 去除首尾空白字符 ```regex ^\s*|\s*$ ``` 此...
3. **网页数据抓取**:工具通过网络请求获取网页内容,然后使用正则表达式对网页文本进行解析,找到符合规则的QQ号码。这通常涉及到HTTP/HTTPS协议的理解,以及HTML文档结构的解析。 4. **数据处理与存储**:搜集到...
### C#网页内容抓取:利用xmlhttp与正则表达式 #### 一、引言 随着互联网技术的发展,大量的信息被存储在网络之中。对于个人开发者或是企业来说,能够高效地从网页上提取所需的数据变得至关重要。网页内容抓取...
该源码的目的是从网络上的各种信息中,通过正则表达式高效地定位和提取这些名人的QQ号码。 在深入分析这个源码之前,我们需要了解易语言的基本语法和结构,如变量定义、控制流程、函数调用等。同时,对于正则表达式...
正则表达式(Regular Expression,简称regex)是用于在文本中匹配特定模式的强大工具,它在编程、数据处理和文本分析等领域中广泛应用。这个“正则表达式资料大全压缩包”很可能包含了各种教程、示例、参考手册以及...
正则表达式是编程中处理文本的强大工具,尤其在网页前端开发中,它常用于数据验证、内容提取和格式化。以下是一些具体的正则表达式实例及其应用: 1. **截取中间字符串**: 示例中的 `preg_match` 函数用于在字符...
通过这些知识点,开发者可以熟练地在Java程序中运用正则表达式进行各种文本处理任务,如数据验证、信息提取等。在实际开发中,结合Java的IO和集合框架,正则表达式可以成为强大的文本处理工具。
// 使用正则表达式验证QQ号码 boolean isValid = qq.matches("[1-9]\\d{4,11}"); if (isValid) { System.out.println("QQ号码正确"); } else { System.out.println("QQ号码不正确,请检查输入"); } } } ```...
虽然这并不是一篇完整的教程,但它提供了一系列实用的正则表达式实例,如用于判断电子邮件地址、QQ号码、身份证号码格式等。对于需要在实际项目中运用正则表达式的开发者来说,这些实例是非常有价值的参考资料。 ##...
正则表达式(Regular Expression)是用于匹配字符串模式的一种语法,广泛应用于文本处理、数据验证、数据提取等领域。以下是对一些常见的正则表达式的详细解释: 1. 匹配中文字符:`[u4e00-u9fa5]` 这个正则表达式...
- 在文本处理过程中过滤掉或提取中文内容。 - 文档或网页内容分析时提取特定的中文词汇。 ### 2. 匹配 HTML 标记 **正则表达式**:`<(\S*?)[^>]*>.*?|*? />` **应用场景**: - 清洗HTML文本中的标签,以便进行文本...
### 正则表达式示例PDA #### 概述 正则表达式是一种强大的文本模式匹配工具,广泛应用于各种编程语言中进行字符串处理、表单验证等工作。本文汇总了一系列常用的正则表达式实例,旨在帮助读者更好地理解和使用正则...
以上正则表达式在处理文本数据时非常实用,尤其在数据清洗、验证用户输入、爬虫抓取信息等领域。在实际应用中,根据具体需求可能会对它们进行调整和优化。例如,在处理HTML时,可能需要更复杂的正则来解析嵌套标签;...
根据提供的文档内容,我们可以深入探讨以下几类正则表达式的应用案例: ##### 1. 匹配中文字符 **表达式**: `[\u4e00-\u9fa5]` - **应用场景**: 在处理包含中文的文本时,此表达式可用于验证或提取文本中的中文字符...
正则表达式是一种强大的文本处理工具,能够帮助我们在文本中查找、替换或提取特定模式的字符串。本文将根据提供的部分内容,详细解读各种常见且实用的正则表达式,并对每个表达式的功能、应用场景以及注意事项进行...
它可以识别文本中的字符串模式,执行搜索、替换、数据提取等多种文本操作。正则表达式可以跨越不同的操作系统和编程语言使用,因为大多数现代编程语言都支持正则表达式。 正则表达式的核心构成元素是元字符和字符集...
10. **匹配QQ号**:`[1-9][0-9]{4,}` - 匹配腾讯QQ号码,从10000开始。 11. **匹配邮政编码**:`[1-9]d{5}(?!d)` - 用于验证6位的中国邮政编码。 12. **匹配身份证号**:`d{15}|d{18}` - 匹配15位或18位的中国...
- 提取纯文本内容时去除HTML标记。 - 清洗用户提交的数据避免XSS攻击。 #### 五、去除字符串两端空白字符 **表达式:** `^\s*|\s*$` **解释:** 该表达式用于去除字符串开头和结尾的所有空白字符。`^\s*`表示匹配...
- **解析:** 这个范围包含了所有常用的中文汉字,适合用来验证或提取文本中的中文内容。 **2. 匹配HTML标签** - **正则表达式:** <(s*?)[^>]*>.*?|*?/> - **解析:** 这个表达式用于匹配HTML标签。它能够识别并匹配...