简体   繁体   中英

How to access the next element below in HTML file using beautiful soup

<ns1:AffectedAreas>
      <ns1:Area>
        <ns1:AreaId>10YDK-1--------W</ns1:AreaId>
        <ns1:AreaName>DK1</ns1:AreaName>
      </ns1:Area>
</ns1:AffectedAreas>

I've been trying my best to access the ns1:AreaId which is (10YDK-1--------W) through ns1:AffectedAreas by using B = soup.find('ns1:area') and then B.next_element but all I get is an empty string .

在此处输入图像描述

You can try to iterate over the soup.find('ns1:area') childrens to find the ns1:areaid tag and then to get his text.

for i in soup.find('ns1:area').children:
    if i.name == "ns1:areaid":
        b = i.text
print(b)

And from ns1:AffectedAreas it will look like

for i in soup.find_all('ns1:AffectedAreas'.lower()):
    for child in i.children:
        if child.name == "ns1:area":
            for y in child.children:
                if y.name == "ns1:areaid":
                    print(y.text)

Or to search the tag ns1:AreaId in lower case and to get text of him. this way you can get all the text values from all ns1:AreaId tags.

soup.find_all("ns1:AreaId".lower())[0].text

Both cases will output

"10YDK-1--------W"

Try this method,

import bs4
import re

data = """
<ns1:AffectedAreas>
      <ns1:Area>
        <ns1:AreaId>10YDK-1--------W</ns1:AreaId>
        <ns1:AreaName>DK1</ns1:AreaName>
      </ns1:Area>
</ns1:AffectedAreas>
"""
def striphtml(data):
    p = re.compile(r'<.*?>')
    return p.sub('', data)

bs = bs4.BeautifulSoup(data, "html.parser")
areaid = bs.find_all('ns1:areaid')
print((striphtml(str(areaid))))

Here, striphtml function will remove all the tags containing <> .So the output will be,

[10YDK-1--------W]

If you have defined namespaces in your HTML/XML document, you can use xml parser and CSS selectors.

For example:

txt = '''<root xmlns:ns1="some namespace">
    <ns1:AffectedAreas>
      <ns1:Area>
        <ns1:AreaId>10YDK-1--------W</ns1:AreaId>
        <ns1:AreaName>DK1</ns1:AreaName>
      </ns1:Area>
</ns1:AffectedAreas>
</root>'''

soup = BeautifulSoup(txt, 'xml')

area_id = soup.select_one('ns1|AffectedAreas ns1|AreaId').text
print(area_id)

Prints:

10YDK-1--------W

Another method.

from simplified_scrapy import SimplifiedDoc, req, utils
html = '''
<ns1:AffectedAreas>
      <ns1:Area>
        <ns1:AreaId>10YDK-1--------W</ns1:AreaId>
        <ns1:AreaName>DK1</ns1:AreaName>
      </ns1:Area>
      <ns1:Area>
        <ns1:AreaId>10YDK-2--------W</ns1:AreaId>
        <ns1:AreaName>DK2</ns1:AreaName>
      </ns1:Area>
</ns1:AffectedAreas>
'''
doc = SimplifiedDoc(html)
AffectedArea = doc.select('ns1:AffectedAreas')
Areas =  AffectedArea.selects('ns1:Area')
AreaIds = Areas.select('ns1:AreaId').html
print (AreaIds)
# or
# print (doc.select('ns1:AffectedAreas').selects('ns1:Area').select('ns1:AreaId').html)

Result:

['10YDK-1--------W', '10YDK-2--------W']

Here are more examples: https://github.com/yiyedata/simplified-scrapy-demo/tree/master/doc_examples

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM