簡體   English   中英

PHP正則表達式轉換為Python代碼

[英]php regular expression convert to python code

我有用php編寫的這段代碼,我想將其轉換為python代碼

$title_regex = "/<title>(.+)<\/title>/i";
preg_match_all($title_regex, $string, $title, PREG_PATTERN_ORDER);
$url_title = $title[1];

/// fecth decription
$tags = get_meta_tags($url);

// fetch images
$image_regex = '/<img[^>]*'.'src=[\"|\'](.*)[\"|\']/Ui';
preg_match_all($image_regex, $string, $img, PREG_PATTERN_ORDER);
$images_array = $img[1];

我已經嘗試過這個..但是它在圖片部分給了我錯誤

import re
out=Data #web site html page ..
title_regex = "/<title>(.+)<\/title>/i" #no need for this .. un used 
m = re.search("<title>(.+)<\/title>", out)
print "title",m.group(1)
#for pics i have tried this but it give me error ..
pics = re.match(r"/<img[^>]*'.'src=[\"|\'](.*)[\"|\']/Ui", out)#the conversion is not correct  
print "grop",pics.group(1)

我的完整代碼

import re
import urllib
print "Start"
url="http://www.deviantart.com"
data=urllib.urlopen(url)
out=data.read()
print 
title_regex = "/<title>(.+)<\/title>/i"
m = re.search("<title>(.+)<\/title>", out)
print "first",m
print "title=",m.group(1)

title_regex = "/<title>(.+)<\/title>/i"

pics = re.match(r"/<img[^>]*src=[\"|\'](.*)[\"|\']/Ui", out)

print "pics>>",pics.group(1)

我如何將php re >>“ /] *'。'src = \\” | \\'[\\“ | \\'] / Ui”轉換為python re?

正則表達式可能什么也沒找到。

嘗試以下操作:最后也刪除/ Ui

import re
out=Data #web site html page ..
title_regex = "/<title>(.+)<\/title>/i" #no need for this .. un used 
if m is not None:  #  NEW  <----------------
   m = re.search("<title>(.+)<\/title>", out)
print "title",m.group(1)
#for pics i have tried this but it give me error ..
pics = re.match(r"<img[^>]*src=[\"|\'](.*)[\"|\']", out)
if pics is not None: # NEW <----------------
   print "grop",pics.group(1)

給你第二個問題試試這個

for filename in pics.groups():
    print filename

工作版本..使用標簽IMG src>代碼顯示給定網站上的所有圖像:

  import re
  import urllib
  print "Start"
  url="http://www.deviantart.com"
  data=urllib.urlopen(url)
  out=data.read()
  print 
  title_regex = "/<title>(.+)<\/title>/i"
  m = re.search("<title>(.+)<\/title>", out)
  print "first",m
  print "grop",m.group(1)

  title_regex = "/<title>(.+)<\/title>/i"

  pics = re.compile(r"<IMG[^>]*src=([^>]*[^/])")#Change IMG tag 
  allpics=pics.findall(out)
  print "found",pics
  for mypic in allpics:
     print "< IMG src=",mypic

謝謝大家

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM