Python,匹配长度不均匀的刮削列表
请准备长时间阅读。我停滞不前,不知道从哪里寻找答案/还有什么可以尝试的。不用说,我对编程有点陌生。在过去的几周里,我一直在做这个项目 问题 我有这张表,25行,2列。每一行的结构如下: 必要事件Python,匹配长度不均匀的刮削列表,python,regex,beautifulsoup,Python,Regex,Beautifulsoup,请准备长时间阅读。我停滞不前,不知道从哪里寻找答案/还有什么可以尝试的。不用说,我对编程有点陌生。在过去的几周里,我一直在做这个项目 问题 我有这张表,25行,2列。每一行的结构如下: 必要事件 第二次尝试解决问题 import mechanize import re htmlA1 = br.response().read() patAttackDate = re.compile('<td align=center>(\d+/\d+/\d+)<br>(\d+:\
第二次尝试解决问题
import mechanize
import re
htmlA1 = br.response().read()
patAttackDate = re.compile('<td align=center>(\d+/\d+/\d+)<br>(\d+:\d+:\d+ \w+)')
patAttackName = re.compile('<font color=#006633>(\w+)</font></a> hospitalized ')
searchAttackDate = re.findall(patAttackDate, htmlA1)
searchAttackName = re.findall(patAttackName, htmlA1)
pairs = zip(searchAttackDate, searchAttackName)
for i in pairs:
print (i)
因此,我想从整个页面中删除换行符
,并删除表格,但是:
import mechanize
import re
from BeautifulSoup import BeautifulSoup
htmlA1 = br.response().read()
stripped = htmlA1.replace(">\n<","><") #Removed all '\n' from code
soup = BeautifulSoup(stripped)
table = soup.find('table', width='90%')
table2 = table.findNext('table', width='90%')
table3 = table2.findNext('table', width='90%') #this is the table I need to work with
patAttackDate = re.compile('<td align="center">(\d+/\d+/\d+)<br />(\d+:\d+:\d+ \w+)')
searchAttackDate = re.findall(patAttackDate, table3)
print searchAttackDate
我错过了什么
奖金问题:
有没有办法说明XID是一个动态变量,但在使用regex/beautifulsoup(或其他刮削方法)时绕过它?随着项目的“增长”,我可能需要包含代码的XID部分,但不想与之匹配。(不确定这是否清楚)
谢谢你抽出时间
编辑1:添加列表示例
编辑2:使代码分隔更加可见
编辑3:为似乎不起作用的给定解决方案添加了示例代码
Test = '''<table><tr><td>date</td></tr></table>'''
soupTest = BeautifulSoup(Test)
test2 = soupTest.find('table')
patTest = re.compile('<td>(.*)</td>')
searchTest = patTest.findall(test2.getText())
print test2 # gives: <table><tr><td>date</td></tr></table>
print type(test2) # gives: <class 'BeautifulSoup.Tag'>
print searchTest #gives: []
编辑5-一个更简单的解决方案(第一次尝试时-没有Beautifulsoup)
.findNext()
方法将返回一个BeautifulSoup.Tag
对象,该对象不能传递给re.findall
。因此,您需要使用.getText()
(或者使用类似的方法从标记
对象获取文本。或者使用.contents
获取标记内部的html)。另外,当使用re.compile
时,返回的对象就是您需要调用的findall
对象
这:
将正则表达式模式编译为正则表达式对象
这个:result=re.match(模式、字符串)
相当于:prog=re.compile(模式)
result=prog.match(字符串)
根据你的描述,我还没有弄清楚你到底想做什么。但我现在可以告诉你一件事:对于正则表达式,Python原始字符串是你的朋友 尝试在BeautifulSoup程序中使用
r'pattern'
,而不仅仅是'pattern'
此外,当您使用正则表达式时,有时从简单模式开始,验证它们是否有效,然后构建它们是很有价值的。您直接使用了复杂的模式,我确信它们不起作用,因为您没有使用原始字符串,反斜杠也不正确。对于beautifulsoup解决方案,您可以使用它(无需检查正则表达式-我也确信@steveha关于addin r''的说法是正确的): 这对我很有用:
reAttack = r'<td\s+align=center>(\d+/\d+/\d+)<br>(\d+:\d+:\d+\s+\w+)</td>\s*<td.*?<font\s+color=#006633>(\w+)</font></a>\s+hospitalized\s+'
for m in re.finditer(reAttack, htmlA1):
print 'date: %s; time: %s; player: %s' % (m.group(1), m.group(2), m.group(3))
reAttack=r'(\d+/\d+/\d+)
(\d+:\d+:\d+\s+\w+\s*)*
在当前TD元素中包含匹配项
仅供参考,您的样本数据存在一些不一致之处。一些属性值被引用,而大多数属性值没有被引用,并且混合了
和
标记。我为我的演示规范化了所有内容,但如果这是真实数据的代表,则需要更复杂的正则表达式。或者,您可以切换到纯DOM解决方案,这在一开始可能会更容易 难怪你有问题。您正在HTML上使用正则表达式。您能为需要的情况阐明规则吗?因为我不确定在python中使用direct正则表达式是解决这个问题的最好方法,而且我也不明白你的确切日期问题,我只是在等着看有多少人忽视了你正在使用BeautifulSoup的事实,只是发布了这个链接@alonisser,我举了一个ouput@chown的答案也是有用的(免费的)!r'Patren'没有切断它。同样的错误。模式可以工作,但即使不工作,也会返回[],而不是错误。我从来没有说过原始字符串可以解决所有问题。但我确实声称,除非使用原始字符串,否则反斜杠内容将无法按您期望的方式工作。您或者需要将每个反斜杠加倍,或者使用原始字符串。searchAttackDate=re.findall(patAttackDate,htmlA1)工作正常。。。我已经发布了两个解决问题的不同尝试,每个尝试都使用了不同的变量。看起来我需要让代码分离更明显。对不起,这是我的错mixup@k3rb3r05哦,好的。但是,在问题的底部,错误:return\u compile(pattern,flags)。findall(string)TypeError:expected string或buffer
是因为您需要像这样调用findall
:patAttackDate.findall(表3)
。同样的错误。我猜是表3(因为它是一个美丽的副产品)把它弄得乱七八糟。如果我使用htmlA1,它可以很好地工作,但是我不会从中删除HTMLnewline@k3rb3r05嗯,如果它仍然给出相同的TypeError
表示它需要一个字符串或缓冲区,那么这意味着table3
不是一个字符串。可能是None
?您可以在该调用的正上方放置类似于打印类型(表3)
的内容以确保。@k3rb3r05对不起,我在意外事件中给了您错误的方法。在edit3中,将patTest.findall(test2.getText())
更改为patTest.findall(str(test2.contents[0]))
。我得到以下错误:searchAttackDate=table3.findall(patAttackDate)TypeError:“NoneType”对象不可调用一个简单得多的解决方案。谢谢顺便说一句,引用的数据和不同的标记是在第二次尝试中使用BeautifulSoup的结果。同时回答了奖金问题。我的印象是,解析的数据必须按顺序(即,如果不在两者之间包含userID,我就无法获取-日期、时间、用户)
(('19/11/11', '9:47:51 PM'), 'user1') <- mismatch
(('19/11/11', '8:21:18 PM'), 'user1') <- mismatch
(('19/11/11', '7:33:00 PM'), 'user1') <- As a consequence of the below, the rest upwards are mismatched
(('19/11/11', '7:32:38 PM'), 'user2') <- NOT a match, case B
(('19/11/11', '7:32:22 PM'), 'user2') <- match ok
(('19/11/11', '7:26:53 PM'), 'user2') <- match ok
(('19/11/11', '7:25:24 PM'), 'user3') <- match ok
(('19/11/11', '7:24:22 PM'), 'user3') <- match ok
(('19/11/11', '7:23:25 PM'), 'user3') <- match ok
import mechanize
import re
from BeautifulSoup import BeautifulSoup
htmlA1 = br.response().read()
stripped = htmlA1.replace(">\n<","><") #Removed all '\n' from code
soup = BeautifulSoup(stripped)
table = soup.find('table', width='90%')
table2 = table.findNext('table', width='90%')
table3 = table2.findNext('table', width='90%') #this is the table I need to work with
patAttackDate = re.compile('<td align="center">(\d+/\d+/\d+)<br />(\d+:\d+:\d+ \w+)')
searchAttackDate = re.findall(patAttackDate, table3)
print searchAttackDate
return _compile(pattern, flags).findall(string)
TypeError: expected string or buffer
Test = '''<table><tr><td>date</td></tr></table>'''
soupTest = BeautifulSoup(Test)
test2 = soupTest.find('table')
patTest = re.compile('<td>(.*)</td>')
searchTest = patTest.findall(test2.getText())
print test2 # gives: <table><tr><td>date</td></tr></table>
print type(test2) # gives: <class 'BeautifulSoup.Tag'>
print searchTest #gives: []
import re
import mechanize
from BeautifulSoup import BeautifulSoup
htmlA1 = br.response().read()
stripped = htmlA1.replace(">\n<","><") #stripped '\n' from html
soup = BeautifulSoup(stripped)
table = soup.find('table', width='90%')
table2 = table.findNext('table', width='90%')
table3 = table2.findNext('table', width='90%') #table I need to work with
print type(table3) # gives <class 'BeautifulSoup.Tag'>
strTable3 = str(table3) #convert table3 to string type so i can regex it
patFinal = re.compile(('(\d+/\d+/\d+)<br />(\d+:\d+:\d+ \w+)</td><td align="center">'
'<font color="#006633"><a href="profiles.php\?XID=(\d+)">'
'<font color="#006633">(\w+)</font></a> hospitalized <a'), re.IGNORECASE)
searchFinal = re.findall(patFinal, strTable3)
for i in searchFinal:
print (i)
('19/11/11', '1:08:07 AM', 'ID_user1', 'user1')
('19/11/11', '1:06:55 AM', 'ID_user1', 'user1')
('19/11/11', '1:05:46 AM', 'ID_user1', 'user1')
('19/11/11', '1:04:33 AM', 'ID_user1', 'user1')
('19/11/11', '1:03:32 AM', 'ID_user1', 'user1')
('19/11/11', '1:02:37 AM', 'ID_user1', 'user1')
('19/11/11', '1:00:43 AM', 'ID_user1', 'user1')
('19/11/11', '12:55:35 AM', 'ID_user2', 'user2')
import re
reAttack = (r'<td\s+align=center>(\d+/\d+/\d+)<br>(\d+:\d+:\d+\s+\w+)</td>\s*'
'<td.*?' #accounts for the '\n'
'<font\s+color=#006633>(\w+)</font></a>\s+hospitalized\s+')
for m in re.finditer(reAttack, htmlA1):
print 'date: %s; time: %s; player: %s' % (m.group(1), m.group(2), m.group(3))
date: 19/11/11; time: 1:08:07 AM; player: user1
date: 19/11/11; time: 1:06:55 AM; player: user1
date: 19/11/11; time: 1:05:46 AM; player: user1
date: 19/11/11; time: 1:04:33 AM; player: user1
date: 19/11/11; time: 1:03:32 AM; player: user1
date: 19/11/11; time: 1:02:37 AM; player: user1
date: 19/11/11; time: 1:00:43 AM; player: user1
date: 19/11/11; time: 12:55:35 AM; player: user2
soup = BeautifulSoup(stripped)
table = soup.find('table', width='90%')
table2 = table.findNext('table', width='90%')
table3 = table2.findNext('table', width='90%') #this is the table I need to work with
patAttackDate = re.compile('<td align="center">(\d+/\d+/\d+)<br />(\d+:\d+:\d+ \w+)')
searchAttackDate = re.findall(patAttackDate, table3)
soup = BeautifulSoup(stripped)
table = soup.find('table', width='90%')
table2 = table.findNext('table', width='90%')
table3 = table2.findNext('table', width='90%')
patAttackDate = re.compile('<td align="center">(\d+/\d+/\d+)<br />(\d+:\d+:\d+ \w+)')
searchAttackDate = patAttackDate.findall(table3.getText())
# or, to search the html inside table3 and not just the text
# searchAttackDate = patAttackDate.findall(str(table3.contents[0]))
re.compile(pattern, flags=0)
searchAttackDate = table3.findAll(patAttackDate)
for row in searchAttackDate:
print row
reAttack = r'<td\s+align=center>(\d+/\d+/\d+)<br>(\d+:\d+:\d+\s+\w+)</td>\s*<td.*?<font\s+color=#006633>(\w+)</font></a>\s+hospitalized\s+'
for m in re.finditer(reAttack, htmlA1):
print 'date: %s; time: %s; player: %s' % (m.group(1), m.group(2), m.group(3))