
1 lhj2100 2012-01-11 21:58:06 +08:00 不明白楼主的需求 用空格替换<p></p>不就可以了么.. |
2 hanbaoo OP 那正则表达式怎么写呢? |
4 qlqsh 2012-01-11 22:04:55 +08:00 记不清了,好久没碰正则了,你找篇文章看下吧,特简单。10分钟基本就解决了。 |
5 gfreezy 2012-01-11 22:07:53 +08:00 不明白需求 +1 如果只是把所有的 <p></p>直接的内容匹配出来 regrex = r'<p>(.*?)</p>' paragraphs = re.findall(regrex, html_content) 应该可以,没测试。 |
7 gfreezy 2012-01-11 22:16:35 +08:00 |
9 chuck911 2012-01-11 22:39:16 +08:00 pyquery/phpquery 类jquery语法 我抓取都用这种,懒得写正则 |
10 gfreezy 2012-01-11 22:42:04 +08:00 @fature beautifulsoup,xpath+lxml效率不低啊,况且一个页面,再大也没多少数据啊。 phantomjs是直接用Qt的webkit内核,跟浏览器差不多,不会效率很差啊。 |
11 evlos 2012-01-11 22:48:51 +08:00 preg_match_all('/<p>(.*?)<\/p>/i',$data,$result); //PHP的,楼上已经有Python的了 |
13 lyxint 2012-01-11 23:00:11 +08:00 python的要加re.DOTALL这个flag, 不然会漏结果 |
14 lyxint 2012-01-11 23:03:03 +08:00 用张教主的txt_wrap_by_all def txt_wrap_by(begin, end, html): if not html: return '' start = html.find(begin) if start >= 0: start += len(begin) end = html.find(end, start) if end >= 0: return html[start:end].strip() def txt_wrap_by_all(begin, end, html): if not html: return '' result = [] from_pos = 0 while True: start = html.find(begin, from_pos) if start >= 0: start += len(begin) endpos = html.find(end, start) if endpos >= 0: result.append(html[start:endpos].strip()) from_pos = endpos+len(end) continue break return result ps = txt_wrap_by_all('<p>', '</p>', html) |
15 lyxint 2012-01-11 23:04:52 +08:00 <script src="https://gist.github.com/1595083.js"> </script> |
16 lyxint 2012-01-11 23:05:14 +08:00 |
17 lyxint 2012-01-11 23:05:39 +08:00 |
19 flyingkid 2012-01-11 23:08:15 +08:00 |
21 hanbaoo OP 谢谢ls >_< |
22 ywjno 2012-01-11 23:17:01 +08:00 正则<p>(.*?)</p>的效率不低的 |
24 lyxint 2012-01-12 00:03:46 +08:00 @fature 字符串下标操作比re要慢??? 不可能的. 让数据说话. http://gist.github.com/1595083.js?file=test.py http://gist.github.com/1595083.js?file=result |
25 lyxint 2012-01-12 00:07:31 +08:00 index.html是wget http://news.sina.com.cn/ 存到本地的 |
26 kojp 2012-01-12 00:22:42 +08:00 手机党伤不起 ........ Php.的话 ,我经常加个 ius之类的多试几次 就能搞定 Python的话 ,结合 beautiful soupv用 还不错 .... |