導航:首頁 > 編程語言 > pythonre模塊例子

pythonre模塊例子

發布時間:2022-06-05 09:57:14

python re模塊中 (P) (P=name) 及 \g<name> 三者的使用區別

題主你好,

沒有單獨的(?P)這種用法, 猜測應該指的是(?P<name>), (?P=name),g<name>這三者的用法.

首先說(?P<name>),它其實和單個圓括弧,(),本質上一樣, 只不過在後面引用分組中多了一種引用方法:

(123)對於這個分組, 你引用時只能是1(這種也是我們最常用的分組與引用的方法),見例子:

=====

希望可以幫到題主, 歡迎追問.

㈡ python 中的問題 關於re模塊

importre
str10="."
str10_list=str10.split()
pattern=re.compile(r"(?P<match_word>The)",re.I)#/<match_word>
print("output#39:")
forwordinstr10_list:
ifpattern.search(word):
print("{:s}".format(pattern.search(word).group('match_word')))

這樣就對了


㈢ 誰用過python中的re來抓取網頁,能否給個例子,謝謝

這是我寫的一個非常簡單的抓取頁面的腳本,作用為獲得指定URL的所有鏈接地址並獲取所有鏈接的標題。

===========geturls.py================
#coding:utf-8
import urllib
import urlparse
import re
import socket
import threading

#定義鏈接正則
urlre = re.compile(r"href=[\"']?([^ >\"']+)")
titlere = re.compile(r"<title>(.*?)</title>",re.I)

#設置超時時間為10秒
timeout = 10
socket.setdefaulttimeout(timeout)

#定義最高線程數
max = 10
#定義當前線程數
current = 0

def gettitle(url):
global current
try:
content = urllib.urlopen(url).read()
except:
current -= 1
return
if titlere.search(content):
title = titlere.search(content).group(1)
try:
title = title.decode('gbk').encode('utf-8')
except:
title = title
else:
title = "無標題"
print "%s: %s" % (url,title)
current -= 1
return

def geturls(url):
global current,max
ts = []
content = urllib.urlopen(url)
#使用set去重
result = set()
for eachline in content:
if urlre.findall(eachline):
temp = urlre.findall(eachline)
for x in temp:
#如果為站內鏈接,前面加上url
if not x.startswith("http:"):
x = urlparse.urljoin(url,x)
#不記錄js和css文件
if not x.endswith(".js") and not x.endswith(".css"):
result.add(x)
threads = []
for url in result:
t = threading.Thread(target=gettitle,args=(url,))
threads.append(t)
i = 0
while i < len(threads):
if current < max:
threads[i].start()
i += 1
current += 1
else:
pass

geturls("http://www..com")

使用正則表達式(re)只能做到一些比較簡單或者機械的功能,如果需要更強大的網頁分析功能,請嘗試一下beautiful soup或者pyquery,希望能幫到你

㈣ python 的 re模塊中如何使用變數代替要匹配的字元串

這么試試:
XH=raw_input("請輸入你的手機型號:")
XH_re=re.compile(XH+'.*?¥(d{1,4})</em>',re.DOTALL)

㈤ python中re模塊的compile函數應該怎麼用

這裡面表示的是一個正則表達式語句的啦,http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html
參考這個看看吧

㈥ 關於python的re正則模塊

樓上已經發了,我刪掉我的回答了

㈦ 在python中模塊是個什麼概念能用簡單的例子說明嗎

就是調用別人編好的函數,自己只要知道用法不用知道內容。比如正則表達式模塊:re

#!/usr/bin/python
import re
#import之後就可以用了
re0=re.complie(r'asdf')
re0.findall('adsfqwerdgfhdsfasd')
。。。。。

閱讀全文

與pythonre模塊例子相關的資料

熱點內容
生意不忙怎麼解壓 瀏覽:500
歡太健康app在哪裡下載 瀏覽:488
androidtools使用教程 瀏覽:971
十天突破雅思口語pdf劍9 瀏覽:295
李誕笑場pdf 瀏覽:265
自用紙巾做解壓筆 瀏覽:129
銀行流水解壓碼是多少 瀏覽:895
百度哪個app好用 瀏覽:316
115廣告聯盟源碼 瀏覽:494
聯通app簽到源碼 瀏覽:680
怎麼連接另一個伺服器的資料庫 瀏覽:742
貓盤洗白命令 瀏覽:844
168api源碼 瀏覽:967
不同地方的文件伺服器如何通信 瀏覽:599
自己電腦上搭建ftp伺服器地址 瀏覽:977
程序員的秋招秘籍 瀏覽:821
單片機國內外研究現狀 瀏覽:308
gta5如何進千人真實伺服器 瀏覽:996
電腦html壁紙源碼 瀏覽:726
c語言返回命令 瀏覽:933