リンク内のすべてのURLリンクを取得するために動的Webクローラーを構築しようとしています。 これまでの章のすべてのリンクを取得することができましたが、各章のセクションリンクを実行しようとすると、出力が何も出力されません。BeautifulSoupを使用しているPython Webクローラー、URLを取得するのに問題がある
私が使用したコード:
#########################Chapters#######################
import requests
from bs4 import BeautifulSoup, SoupStrainer
import re
base_url = "http://law.justia.com/codes/alabama/2015/title-{title:01d}/"
for title in range (1,4):
url = base_url.format(title=title)
r = requests.get(url)
for link in BeautifulSoup((r.content),"html.parser",parse_only=SoupStrainer('a')):
if link.has_attr('href'):
if 'chapt' in link['href']:
href = "http://law.justia.com" + link['href']
leveltwo(href)
#########################Sections#######################
def leveltwo(item_url):
r = requests.get(item_url)
soup = BeautifulSoup((r.content),"html.parser")
section = soup.find('div', {'class': 'primary-content' })
for sublinks in section.find_all('a'):
sectionlinks = sublinks.get('href')
print (sectionlinks)
ありがとうございます!私はあなたがしたことを見る。チャプターのために私のパート1の機能を定義し、セクション1のセクションを参照するためにセクション "レベル2"の機能を定義すると、機能するでしょうか? – CHballer
ええ、もしあなたが正しく理解していれば。両方の 'セクション'が関数で囲まれていれば読むのがもっとうまくいくだろう; – n1c9
haha、thats私が言ったもの、もう一度感謝! – CHballer