我有以下html模式我想使用BeautifulSoup來報廢。 html模式是:查詢:美麗的湯+ href模式不像我想要的那樣刮
<a href="link" target="_blank" onclick="blah blah blah">TITLE</a>
我想抓住TITLE和顯示在鏈接中的信息。也就是說,如果您點擊鏈接,則會顯示TITLE的說明。我想要那個描述。
我開始只是想抓住題目與下面的代碼:
import urllib
from bs4 import BeautifulSoup
import re
webpage = urrlib.urlopen("http://urlofinterest")
title = re.compile('<a>(.*)</a>')
findTitle = re.findall(title,webpage)
print findTile
我的輸出是:
% python beta2.py
[]
所以這顯然是連沒有找到稱號。我甚至嘗試過<a href>(.*)</a>
,但沒有奏效。根據我對文檔的閱讀,我認爲BeautifulSoup會抓住我給它的符號之間的任何文本。在這種情況下,那麼我做錯了什麼?
想必應該打印findTitle而不是findTile? –
您編譯的重新編排模式與鏈接不匹配,請嘗試使用re.compile('(。*?)<\/a>')...練習https://regex101.com/ – rebeling