Python正则表达式问题

Python正则表达式问题,python,html,regex,web-scraping,html-parsing,Python,Html,Regex,Web Scraping,Html Parsing,我试图使用python通过urlib扫描页面并使用regex查找代理,从而从一个数据库中获取代理 页面上的代理如下所示: <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.ur

我试图使用python通过urlib扫描页面并使用regex查找代理,从而从一个数据库中获取代理

页面上的代理如下所示:

<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
8080
我的代码如下所示:

<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
对于站点中的站点:
content=urllib.urlopen(site.read)()
e=re.findall(“\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\\\\\d+”,内容)
#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+
对于e中的代理:
s、 附加(代理)
金额+=1
正则表达式:

\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\<\/\a\>\<\/td\>\<td\>\d+
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\\\\d+
我知道代码是有效的,但是正则表达式是错误的

你知道我该怎么解决这个问题吗


编辑:似乎我的正则表达式还可以吗?

一个选项是使用HTML解析器查找IP地址和端口

示例(使用HTML解析器):

印刷品:

80.193.214.231 3128
186.88.37.204 8080
180.254.72.33 80
201.209.27.119 8080
...
[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']
...
这里的想法是找到所有
a
标记,这些标记的文本匹配
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
正则表达式。对于每个链接,查找父级的下一个
td
同级,其文本匹配
\d+


或者,由于您知道表结构以及有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]]
印刷品:

80.193.214.231 3128
186.88.37.204 8080
180.254.72.33 80
201.209.27.119 8080
...
[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']
...

一种选择是使用HTML解析器查找IP地址和端口

示例(使用HTML解析器):

印刷品:

80.193.214.231 3128
186.88.37.204 8080
180.254.72.33 80
201.209.27.119 8080
...
[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']
...
这里的想法是找到所有
a
标记,这些标记的文本匹配
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
正则表达式。对于每个链接,查找父级的下一个
td
同级,其文本匹配
\d+


或者,由于您知道表结构以及有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]]
印刷品:

80.193.214.231 3128
186.88.37.204 8080
180.254.72.33 80
201.209.27.119 8080
...
[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']
...

一种选择是使用HTML解析器查找IP地址和端口

示例(使用HTML解析器):

印刷品:

80.193.214.231 3128
186.88.37.204 8080
180.254.72.33 80
201.209.27.119 8080
...
[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']
...
这里的想法是找到所有
a
标记,这些标记的文本匹配
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
正则表达式。对于每个链接,查找父级的下一个
td
同级,其文本匹配
\d+


或者,由于您知道表结构以及有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]]
印刷品:

80.193.214.231 3128
186.88.37.204 8080
180.254.72.33 80
201.209.27.119 8080
...
[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']
...

一种选择是使用HTML解析器查找IP地址和端口

示例(使用HTML解析器):

印刷品:

80.193.214.231 3128
186.88.37.204 8080
180.254.72.33 80
201.209.27.119 8080
...
[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']
...
这里的想法是找到所有
a
标记,这些标记的文本匹配
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
正则表达式。对于每个链接,查找父级的下一个
td
同级,其文本匹配
\d+


或者,由于您知道表结构以及有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]]
印刷品:

80.193.214.231 3128
186.88.37.204 8080
180.254.72.33 80
201.209.27.119 8080
...
[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']
...

查看
lxml
beautifulsou
。使用regex for html是一种黑客行为。不要转义
,a,/
同样,如果你不想转义regex中的每一个\,你需要使用原始字符串:在sting前面加上
r
,例如
r“\d{1,3}”
该站点甚至有“导出为JSON”和“导出为文本”功能。也许你骑错马了?看看
lxml
beautifulsou
。使用regex for html是一种黑客行为。不要转义
,a,/
同样,如果你不想转义regex中的每一个\,你需要使用原始字符串:在sting前面加上
r
,例如
r“\d{1,3}”
该站点甚至有“导出为JSON”和“导出为文本”功能。也许你骑错马了?看看
lxml
beautifulsou
。使用regex for html是一种黑客行为。不要转义
,a,/
同样,如果你不想转义regex中的每一个\,你需要使用原始字符串:在sting前面加上
r
,例如
r“\d{1,3}”
该站点甚至有“导出为JSON”和“导出为文本”功能。也许你骑错马了?看看
lxml
beautifulsou
。使用regex for html是一种黑客行为。不要转义
,a,/
同样,如果你不想转义regex中的每一个\,你需要使用原始字符串:在sting前面加上
r
,例如
r“\d{1,3}”
该站点甚至有“导出为JSON”和“导出为文本”功能。也许你骑错马了?