Python Beautiful Soup - 如何检测DOM中的两个Tag或NavigableString对象是否相同

时间:2018-03-21 21:40:10

标签: python beautifulsoup

如何使用Beautiful Soup API检查从同一BeautifulSoup对象检索的两个Tag或NavigableString对象是否与DOM中的对象完全相同?

例如,下面的最后一行代码求值为True,但我需要能够检测到first_paragraphsecond_paragraph与DOM中的对象不同。

from bs4 import BeautifulSoup

example_html_str = """\
    <html>
    <head></head>
    <body>
        <p>Content</p>
        <p>Content</p>
    </body>
    </html>
    """

soup = BeautifulSoup(example_html_str, "lxml")
paragraphs = soup.find_all("p")

first_paragraph = paragraphs[0] #First paragraph
second_paragraph = paragraphs[1] #Second paragraph

# The below evaluates to true, but they are not
# from the same part of the DOM.
print(first_paragraph == second_paragraph)

1 个答案:

答案 0 :(得分:1)

假设汤对象总是一个对象(而不是通过不同汤对象处理的同一HTML文档),您应该能够这样做:

func zoomToFitMapAnnotations(aMapView:MKMapView)
{
    if(aMapView.annotations.count == 0)
    {
        return
    }


    var topLeftCoord = CLLocationCoordinate2D.init(latitude: -90, longitude: 180)


    var bottomRightCoord = CLLocationCoordinate2D.init(latitude: 90, longitude: -180)


    for i in 0..<aMapView.annotations.count
    {
        let annotation = aMapView.annotations[i]

        topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
        topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);

        bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
        bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
    }


    let resd = CLLocationCoordinate2D.init(latitude: topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5, longitude: topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5)

    let span = MKCoordinateSpan.init(latitudeDelta: fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.3, longitudeDelta: fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.3)

    var region = MKCoordinateRegion.init(center: resd, span: span);



    region = aMapView.regionThatFits(region)

    aMapView.setRegion(region, animated: true)


}