Код: Выделить всё
[url=http://example.com]
£3.99
£3.59
1 Feb 2013
[h4]A title[/h4]
[img]http://example.com/image.jpg[/img]
A, List, Of, Terms, To, Extract - 1 Feb 2013
[/url]
У меня есть этот код:
Код: Выделить всё
from BeautifulSoup import BeautifulSoup
with open("file.html") as fp:
html = fp.read()
soup = BeautifulSoup(html)
parts = soup.findAll('a', attrs={"class":re.compile('part'), re.IGNORECASE} )
for part in parts:
mypart={}
# ghi
mypart['ghi'] = part.find(attrs={"class": re.compile('ghi')} ).string
# def
mypart['def'] = part.find(attrs={"class": re.compile('def')} ).string
# h4
mypart['title'] = part.find('h4').string
# jkl
mypart['other'] = part.find('p').string
# abc
pattern = re.compile( r'\&\#163\;(\d{1,}\.?\d{2}?)' )
theprices = re.findall( pattern, str(part) )
if len(theprices) == 2:
mypart['price'] = theprices[1]
mypart['rrp'] = theprices[0]
elif len(theprices) == 1:
mypart['price'] = theprices[0]
mypart['rrp'] = theprices[0]
else:
mypart['price'] = None
mypart['rrp'] = None
Я также хочу извлечь две цены из abc, что мой скрипт сейчас делает довольно неуклюже. Иногда в этой части есть две цены, иногда одна, а иногда ни одной.
Наконец, я хочу извлечь «A, List, Of, Условия, Кому, Извлечь»< /code> часть класса jkl, которую мой скрипт не может выполнить. Я думал, что получение строковой части тега p сработает, но не могу понять, почему это не так. Дата в этой части всегда совпадает с датой в классе ghi, поэтому ее легко заменить/удалить.
Какой совет? Спасибо!
Подробнее здесь: https://stackoverflow.com/questions/146 ... -in-python