【发布时间】:2012-01-02 02:29:32
【问题描述】:
请准备好长时间阅读。我处于停顿状态,不知道在哪里寻找答案/还有什么可以尝试的。不用说我对编程有点陌生。过去几周一直在研究这个项目。
问题
我得到了这张表,25 行,2 列。每一行的结构如下:
需要的事件
<td align=center>19/11/11<br>12:01:21 AM</td>
<td align=center><font color=#006633><a href=profiles.php?XID=1><font color=#006633>player1</font></a> hospitalized <a href=profiles.php?XID=2><font color=#006633>player2</font></a></font></td>
不需要的事件 案例A
<td align="center">19/11/11<br />12:58:03 AM</td>
<td align="center"><font color="#AA0000">Someone hospitalized <a href=profiles.php?XID=1><font color="#AA0000">player1</font></a></font></td>
不需要的事件 案例B
<td align="center">19/11/11<br />12:58:03 AM</td>
<td align=center><font color=#006633><a href=profiles.php?XID=3><font color=#006633>player3</font></a> attacked <a href=profiles.php?XID=1><font color=#006633>player1</font></a> and lost </font></td>
我已经使用正则表达式来抓取所需的数据。我的问题是这两个列表没有匹配。日期和时间并不总是与确切的事件匹配。
第一次尝试解决问题
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)
但这让我得到了wrong time - correct event 类型的列表。
例如:
(('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
第二次尝试解决问题
所以想从整个页面中删除newline并刮掉表格,但是:
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
我错过了什么?
奖金问题: 有没有办法解释 XID 是一个动态变量,但在使用 regex / beautifulsoup(或其他抓取方法)时绕过它?随着项目“增长”,我可能需要包含代码的 XID 部分,但不想与之匹配。 (不确定这是否清楚)
感谢您的宝贵时间
EDIT 1:添加列表示例
EDIT 2:使代码分离更加明显
EDIT 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: []
编辑 4 - 解决方案
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')
EDIT 5 - 一个更简单的解决方案(第一次尝试 - 没有 Beautifulsoup)
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
【问题讨论】:
-
难怪你有问题。您在 HTML 上使用正则表达式。
-
您能否阐明所需案例的规则?因为我不确定使用直接正则表达式是在 python 中解决这个问题的最佳方法,而且我不明白你的确切日期问题
-
我只是在等着看看有多少人忽略了您正在使用 BeautifulSoup 的事实并发布此链接。 stackoverflow.com/questions/1732348/…
-
@alonisser,我已经包含了一个输出示例
标签: python regex beautifulsoup