2014-10-04 2 views
0

site에서 프록시를 가져 오려면 urlib로 페이지를 스캔하고 regex를 사용하여 프록시를 찾아 파이썬을 사용하십시오.Python 정규식 문제

내 코드는 다음과 같습니다
<a href="/ip/190.207.169.184/free_Venezuela_proxy_servers_VE_Venezuela">190.207.169.184</a></td><td>8080</td><td> 

:

for site in sites: 
content = urllib.urlopen(site).read() 
e = re.findall("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\<\/\a\>\<\/td\>\<td\>\d+", content) 
#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+ 

for proxy in e: 
    s.append(proxy) 
    amount += 1 

정규식 :

\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\<\/\a\>\<\/td\>\<td\>\d+ 

내가 아는 코드가 있지만 작동

는 는

페이지에 프록시는 다음과 같이 보입니다 정규 표현식이 틀렸다고.

어떻게 해결할 수 있습니까?

편집 : http://www.regexr.com/ 내 정규식은 괜찮습니까?

+3

보세요. html에 regex를 사용하는 것은 해킹입니다. –

+2

탈출하지 마십시오 '<,>, /, http://regex101.com/r/xB5sT0/2 –

+0

참조 http://stackoverflow.com/questions/26183643/find-specific-text-in-beautifulsoup/ 26183877 # 26183877 –

답변

3

하나의 옵션은 HTML 구문 분석기를 사용하여 IP 주소와 포트를 찾는 것입니다.

예 (사용 BeautifulSoup HTML 파서) :

import re 
import urllib2 
from bs4 import BeautifulSoup 

data = urllib2.urlopen('http://letushide.com/protocol/http/3/list_of_free_HTTP_proxy_servers') 

IP_RE = re.compile(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}') 
PORT_RE = re.compile(r'\d+') 

soup = BeautifulSoup(data) 
for ip in soup.find_all('a', text=IP_RE): 
    port = ip.parent.find_next_sibling('td', text=PORT_RE) 
    print ip.text, port.text 

인쇄합니다 :

80.193.214.231 3128 
186.88.37.204 8080 
180.254.72.33 80 
201.209.27.119 8080 
... 

생각이 여기 \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} 정규 표현식과 일치하는 텍스트 모든 a 태그를 찾는 것입니다. 각 링크에 대해 과 일치하는 부모의 다음 td 형제를 찾으십시오. 당신은 테이블 구조와 IP를 및 포트가 열이, 당신은 단지 인덱스로 각 행에서 셀 값을 얻을 수 있습니다 알고 있기 때문에


또는, 필요가 여기에 정규 표현식에 뛰어 없습니다 :

import urllib2 
from bs4 import BeautifulSoup 

data = urllib2.urlopen('http://letushide.com/protocol/http/3/list_of_free_HTTP_proxy_servers') 

soup = BeautifulSoup(data) 
for row in soup.find_all('tr', id='data'): 
    print [cell.text for cell in row('td')[1:3]] 

인쇄 : lxml` 또는`beautifulsoup``에

[u'80.193.214.231', u'3128'] 
[u'186.88.37.204', u'8080'] 
[u'180.254.72.33', u'80'] 
[u'201.209.27.119', u'8080'] 
[u'190.204.96.72', u'8080'] 
[u'190.207.169.184', u'8080'] 
[u'79.172.242.188', u'8080'] 
[u'1.168.171.100', u'8088'] 
[u'27.105.26.162', u'9064'] 
[u'190.199.92.174', u'8080'] 
...