【发布时间】:2014-04-29 18:22:15
【问题描述】:
我写了这段代码来删除像这样匹配的标签
<p><b>See also:</b> <a href=\"(.*?)\">(.*)</a>(.*)</p>
代码:
import mechanize
import urllib2
from bs4 import BeautifulSoup
import re
med = 'paracetamol'
listiterator = []
listiterator[:] = range(2,16)
br = mechanize.Browser()
br.set_handle_robots(False)
r=br.open("http://www.drugs.com/search-wildcard-phonetic.html")
br.select_form(nr=0)
br.form['searchterm'] = med
br.submit()
url = br.response().geturl()
print url
mainurl = urllib2.urlopen(url).read()
subpages = re.findall("<a href=\"(.*?).html\">[^>]*>", mainurl)
for sub in subpages:
if sub.startswith("http:"):
soup = BeautifulSoup(urllib2.urlopen(sub).read())
m = soup.find_all("div", {"class":"contentBox"})
head = m[0].find_all(["h2","p"])
for i in head:
m = re.match("<p><b>See also:</b> <a href=\"(.*?)\">(.*)</a>(.*)</p>").group()
if not m:
print i
break
我收到了这个错误:
m = re.match("<p><b>See also:</b> <a href=\"(.*?)\">(.*)</a>(.*)</p>",i).group()
File "/usr/lib/python2.7/re.py", line 137, in match
return _compile(pattern, flags).match(string)
TypeError: expected string or buffer
【问题讨论】:
标签: python html regex beautifulsoup