簡體   English   中英

Python Beautiful Soup停止解析

[英]Python Beautiful Soup stops parsing

我嘗試通過以下腳本解析附加的text.txt文件(使用html語法)。

#!/usr/bin/python3

import re
from bs4 import BeautifulSoup

pattern = re.compile("www.geocaching.com")
f=open("text.txt")
text=f.read()
f.close()
s = BeautifulSoup(text)
a = s.find_all(href=pattern)
print(len(a))
print (a[len(a)-1])

我期望所有標簽都帶有href =“ www.geocaching.com”,但我無法從附件中獲取全部標簽。 最后一個是:

<a class="lnk " href="http://www.geocaching.com/geocache/GC3HWHJ_corse-known-unknown-2-view-on-ile-de-giraglia"><span>Corse known &amp; unknown 2 - View on Ile de Giraglia</span></a>

如果我刪除僅包含一些簡單html代碼的行626-674,則會得到下兩個,即最后一個是

<a class="lnk " href="http://www.geocaching.com/geocache/GC3MEDG_tour-genoise-dagnello"><span>TOUR GENOISE D'AGNELLO</span></a>

但同樣,我沒有得到可以在html文件中手動找到的所有結果。

我使用的文件來自此處(我已下載該文件以在本地使用) https://www.geocaching.com/seek/nearest.aspx?lat=43.410333&lon=09.0476&dist=100

嘗試通過以下方式使用CSS選擇器:

from bs4 import BeautifulSoup

f = open("text.txt")
text = f.read()
f.close()

soup = BeautifulSoup(text)

# this find all the href containing the text "www.geocaching.com"
links =  soup.select('[href]~="www.geocaching.com"')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM