亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

python爬蟲程序中能否返回指定數量的匹配記錄(re模塊)

python爬蟲程序中能否返回指定數量的匹配記錄(re模塊)

弒天下 2018-10-09 12:19:45
re.findall()返回的是所有匹配記錄
查看完整描述

2 回答

?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

findall()返回的是一個列表,你需要對其中內容進行一步操作時,例如直接訪問或寫入數據庫的時候,遍厲列表時用一個變量限制一下就行了。

還是我理解錯了你的意思?


查看完整回答
反對 回復 2018-10-14
?
慕姐4208626

TA貢獻1852條經驗 獲得超7個贊

import re

 

p = re.compile(r'\d+')

print p.findall('one1two2three3four4')

print p.findall('one1two2three3four4')[0:2]

 

### output ###

# ['1', '2', '3', '4']

# ['1', '2']

你可以用切片操作返回來處理findall返回的結果來達到你的目的

or

import re                                                          


count = 0

# The end point number

endpoint = 2


p = re.compile(r'\d+')

for m in p.finditer('one1two2three3four4'):

    count += 1

    if count > endpoint: break

    print m.group()

    

### output ###

# 1 2


查看完整回答
反對 回復 2018-10-14
  • 2 回答
  • 0 關注
  • 1368 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號