こんにちは、スタック コミュニティです。
ほとんどのヘルプは Python 2.7 用にあるように見えるため、解決できないように見える問題があります。
Web ページからテーブルを取得して、アンカー全体ではなくリンクテキストだけを取得したい。
コードは次のとおりです: from urllib.request import urlopen from bs4 import BeautifulSoup import re
url = 'http://www.craftcount.com/category.php?cat=5'
html = urlopen(url).read()
soup = BeautifulSoup(html)
alltables = soup.findAll("table")
## This bit captures the input from the previous sequence
results=[]
for link in alltables:
rows = link.findAll('a')
## Find just the names
top100 = re.findall(r">(.*?)<\/a>",rows)
print(top100)
実行すると、「TypeError: expected string or buffer」が表示されます
最後の行から 2 番目までは、すべて正しく実行されます (「print(top100)」を「print(rows)」に交換した場合)。
私が得る応答の例として:
<a href="http://www.etsy.com/shop.php?user_id=5323531"target="_blank">michellechangjewelry</a>
そして、私はただ取得する必要があります: michellechangjewelry
pythex.org によると、私の (ir) 正規表現は機能するはずです。追加の問題として、ほとんどの人は別の方法、つまり全文を取得して URL 部分のみを必要とする方法を好むようです。
最後に、私は「便利さ」から BeautifulSoup を使用していますが、解析をリンクテキストに絞り込むためのより良いパッケージを提案していただければ、私はそれに恩義を感じません。
よろしくお願いします!