Reputation: 73
I've converted a PDF to XML for easier parsing. An example of my XML is as follows:
<LTTextLineHorizontal bbox="[58.501, 729.764, 78.501, 737.764]" height="8.0" width="20.0" word_margin="0.1" x0="58.501" x1="78.501" y0="729.764" y1="737.764"><LTTextBoxHorizontal bbox="[58.501, 729.764, 78.501, 737.764]" height="8.0" index="13" width="20.0" x0="58.501" x1="78.501" y0="729.764" y1="737.764">Date: </LTTextBoxHorizontal></LTTextLineHorizontal>
<LTTextLineHorizontal bbox="[107.249, 730.004, 147.281, 738.004]" height="8.0" width="40.032" word_margin="0.1" x0="107.249" x1="147.281" y0="730.004" y1="738.004"><LTTextBoxHorizontal bbox="[107.249, 730.004, 147.281, 738.004]" height="8.0" index="14" width="40.032" x0="107.249" x1="147.281" y0="730.004" y1="738.004">02/26/2020 </LTTextBoxHorizontal></LTTextLineHorizontal>
The "Date:" is on one line and the next sibling (I think that's what it's called) has the value "02/26/2020."
I've tried:
from bs4 import BeautifulSoup
xml = open("test3.xml").read()
soup = BeautifulSoup(xml)
print(soup.find(text="Date:").findNext('LTTextLineHorizontal').contents[0])
I get the error:
AttributeError: 'NoneType' object has no attribute 'findNext'
My goal is to iterate through the XML and pull out the desired variables (i.e. Date) and but them into a dataframe. But I haven't even gotten that far yet.
The output for this could be a dictionary or string of Date: 02/26/2020
Upvotes: 1
Views: 544
Reputation: 33384
Use Regular Expression re
.
import re
from bs4 import BeautifulSoup
xml = open("test3.xml").read()
soup = BeautifulSoup(xml,'lxml')
print(soup.find('lttextboxhorizontal',text=re.compile("Date:")).find_next('lttextboxhorizontal').text)
Or
import re
from bs4 import BeautifulSoup
xml = open("test3.xml").read()
soup = BeautifulSoup(xml,'lxml')
print(soup.find(text=re.compile("Date:")).find_next('lttextboxhorizontal').text)
You can do that without regular expression as well put a space.
from bs4 import BeautifulSoup
xml = open("test3.xml").read()
soup = BeautifulSoup(xml,'lxml')
print(soup.find(text="Date: ").find_next('lttextboxhorizontal').text)
Upvotes: 1