⑴ python 正則如何抓取 <a></a> 中 href 屬性和標簽里的內容
importre
pattern='<a.*?href="(.+)".*?>(.*?)</a>'
withopen("test.html","r")asfp:
forlineinfp:
ret=re.search(pattern,line)
ifret:
forxinret.groups():printx
不知道具體格式是怎樣的,我這里也就簡單舉個例子。
groups獲取到的就是正則pattern裡面( )中的內容,以元組形式返回。
⑵ python 正則獲取網頁內容
importre
#id=45717
common_log_format_regex=re.compile('id=d+')
files=open("aaa.txt",'r',encoding='utf-8')
lines=files.readlines()
txt=''.join(lines)
files.close()
data=common_log_format_regex.findall(txt)
writer=open("id.txt",'w',encoding='utf-8')
writer.write(' '.join(data))
writer.close()
⑶ python 正則表達式 (.*)
groups()返回所有捕獲組構成的tuple。你的正則表達式中有唯一一個捕獲組(.*?),而?在此處表示非貪婪匹配,即在整個正則表達式成立的前提下匹配盡可能少的字元,此處最少的情況是什麼也不匹配,整個正則表達式匹配Python中的Py,而捕獲組自然為空字元串。
⑷ python 正則表達式,怎樣匹配以某個字元串開頭,以某個字元串結尾的情況
匹配以某個字元串開頭,以某個字元串結尾的情況的正則表達式:^abc.*?qwe$
Python正則表達式的幾種匹配用法:
1.測試正則表達式是否匹配字元串的全部或部分
regex=ur""#正則表達式
ifre.search(regex,subject):
do_something()
else:
do_anotherthing()
2.測試正則表達式是否匹配整個字元串
regex=ur"/Z"#正則表達式末尾以/Z結束
ifre.match(regex,subject):
do_something()
else:
do_anotherthing()
3.創建一個匹配對象,然後通過該對象獲得匹配細節(Create an object with details about how the regex matches (part of) a string)
regex=ur""#正則表達式
match=re.search(regex,subject)
ifmatch:
# match start:match.start()
# match end(exclusive):atch.end()
# matched text:match.group()
do_something()
else:
do_anotherthing()
4.獲取正則表達式所匹配的子串(Get the part of a string matched by the regex)
regex=ur""#正則表達式
match=re.search(regex,subject)
ifmatch:
result=match.group()
else:
result=""
5. 獲取捕獲組所匹配的子串(Get the part of a string matched by a capturing group)
regex=ur""#正則表達式
match=re.search(regex,subject)
ifmatch:
result=match.group(1)
else:
result=""
6. 獲取有名組所匹配的子串(Get the part of a string matched by a named group)
regex=ur"" #正則表達式
match = re.search(regex, subject)
if match:
result = match.group"groupname")
else:
result = ""
7. 將字元串中所有匹配的子串放入數組中(Get an array of all regex matches in a string)
result=re.findall(regex,subject)
8.遍歷所有匹配的子串(Iterate over all matches in a string)
formatchinre.finditer(r"<(.*?)/s*.*?//1>",subject)
# match start:match.start()
# match end(exclusive):atch.end()
# matched text:match.group()
9.通過正則表達式字元串創建一個正則表達式對象(Create an object to use the same regex for many operations)
reobj=re.compile(regex)
10.用法1的正則表達式對象版本(use regex object for if/else branch whether (part of) a string can be matched)
reobj=re.compile(regex)
ifreobj.search(subject):
do_something()
else:
do_anotherthing()
11.用法2的正則表達式對象版本(use regex object for if/else branch whether a string can be matched entirely)
reobj=re.compile(r"/Z")#正則表達式末尾以/Z 結束
ifreobj.match(subject):
do_something()
else:
do_anotherthing()
12.創建一個正則表達式對象,然後通過該對象獲得匹配細節(Create an object with details about how the regex object matches (part of) a string)
reobj=re.compile(regex)
match=reobj.search(subject)
ifmatch:
# match start:match.start()
# match end(exclusive):atch.end()
# matched text:match.group()
do_something()
else:
do_anotherthing()
13.用正則表達式對象獲取匹配子串(Use regex object to get the part of a string matched by the regex)
reobj=re.compile(regex)
match=reobj.search(subject)
ifmatch:
result=match.group()
else:
result=""
14.用正則表達式對象獲取捕獲組所匹配的子串(Use regex object to get the part of a string matched by a capturing group)
reobj=re.compile(regex)
match=reobj.search(subject)
ifmatch:
result=match.group(1)
else:
result=""
15.用正則表達式對象獲取有名組所匹配的子串(Use regex object to get the part of a string matched by a named group)
reobj=re.compile(regex)
match=reobj.search(subject)
ifmatch:
result=match.group("groupname")
else:
result=""
16.用正則表達式對象獲取所有匹配子串並放入數組(Use regex object to get an array of all regex matches in a string)
reobj=re.compile(regex)
result=reobj.findall(subject)
17.通過正則表達式對象遍歷所有匹配子串(Use regex object to iterate over all matches in a string)
reobj=re.compile(regex)
formatchinreobj.finditer(subject):
# match start:match.start()
# match end(exclusive):match.end()
# matched text:match.group()
⑸ Python 正則匹配抓取到的網頁超鏈接怎麼URLopen
re.findall返回的是列表,遍歷列表將其值做為參數傳入urlopen即可,類似於下面這樣
for url in re.findall(XXXXXX):
print urlopen(url).read()
⑹ 在python正則表達式中\1是什麼意思
\1
有兩者意義:
1.
如果\1前面有捕獲的分組的表達式即用()括起來的匹配,則
\1
表示對前面第一個捕獲分組內容的引用。例如
([A-Z])567\1表示匹配前後為相同大寫字母包圍567的字串。
2.
如果\1前面沒有捕獲的分組的表達式即用()括起來的匹配,則
\1
表示匹配八進制數字1
⑺ python 正則表達式如何截取字元串中間的內容
啟動ipython先導入re模塊
re 模塊的一般使用步驟如下:
使用 compile 函數將正則表達式的字元串形式編譯為一個 Pattern 對象
通過 Pattern 對象提供的一系列方法對文本進行匹配查找,獲得匹配結果(一個 Match 對象)
最後使用 Match 對象提供的屬性和方法獲得信息,根據需要進行其他的操作
findall 方法的使用形式如下:
findall(string[, pos[, endpos]])
其中,string 是待匹配的字元串,pos 和 endpos 是可選參數,指定字元串的起始和終點位置,默認值分別是 0 和 len (字元串長度)。
findall 以列表形式返回全部能匹配的子串,如果沒有匹配,則返回一個空列表。
⑻ Python正則表達式的非貪婪匹配的原則是什麼
正則表達式匹配中,已經被捕獲的內容不會再次用於匹配測試。
第二個k已經被第一個匹配捕獲,然後就在45k67k中繼續做匹配測試。
⑼ Python正則表達式(二)
上節我們說到 Python 正則表達式的基本字元,以及這些字元的用法
今天,我們繼續講講 Python 中一些擴展標記法,以及一些特殊序列
(?...) : 這種擴展標記法以括弧內 ? 開頭,其後第一個字元決定了採用什麼樣的語法。
在 ? 後面添加( 'a', 'i', 'L', 'm', 's', 'u', 'x' 中的一個或多個),然後加上匹配規則。
這些字元對正則表達式設置以下標記,免去設置 flag 參數
注意 : 'a', 'L', 'u' 作為內聯標記是相互排斥的,它們不能結合在一起
括弧分組的非捕獲版本,該分組所匹配的子字元串 不能 在執行匹配後被獲取或是在之後的模式中被引用
可以配合 | 和 {m} 使用
為分組再指定一個組合名
每個組合名只能用一個正則表達式定義,只能定義一次
反向引用一個命名組合
匹配前面那個名字叫 name 的命名組中匹配到的字元串
注釋信息,裡面的內容會被忽略。
哈哈,是不是沒看懂,沒事,舉個栗子
看看,是不是一下子就明了了。
哈哈,這個又看不懂?
思考一下,既然有根據後面字元斷言的,那麼根據前面字元來斷言,也是很合理的,
如果給定的 id 或 name 存在,將會嘗試匹配 yes-pattern ,否則就嘗試匹配 no-pattern , no-pattern 可選,也可以被忽略。
是不是有點像 if else 三目運算,其中 id 和 name 是分組 id 、和指定的分組名 name
照舊,舉個栗子吧
看了栗子是不是有點糊塗呢,我們來解析一下這個正則表達式
其結果匹配的就是 <[email protected]> 和 [email protected] 。
而不會匹配 <[email protected] ' 和 <[email protected]
但是上面的第三個結果為啥不一樣呢?
因為 findall 允許返回空匹配的,在有 ? 的情況下,所以它會分兩種情況去匹配
今天講了一些擴展標記法,其實沒那麼難,多看看例子,多練習練習。
下節將介紹 re 模塊各函數的用法,敬請期待......
⑽ Python 正則 獲取文本中匹配內容
正則表達式:(?<=d+.)[sS]+?(?=d+|$)
我給你個java語言的例子:
publicclassAEF{
publicstaticvoidmain(String[]args){
Strings="12.ewq example fdsfdf fd中文 13.wer fdsfd 例子 14.qrew 發的薩芬的 fdsfs 15.fwewq 范德薩范德薩";
Stringregex="(?<=\d+\.)[\s\S]+?(?=\d+|$)";
Patternp=Pattern.compile(regex);
Matcherm=p.matcher(s);
while(m.find()){
System.out.println(m.group());
}
}
}
運行結果:
ewq
example
fdsfdf
fd中文
wer
fdsfd
例子
qrew
發的薩芬的
fdsfs
fwewq
范德薩范德薩