BeautifulSoup関連リファレンス

BeautifulSoup

soupを生成


from bs4 import BeautifulSoup

response = requests.get(url)
response.encoding = response.apparent_encoding
soup = BeautifulSoup(response.text, "html.parser")

諸々


from bs4 import BeautifulSoup

response.encoding = response.apparent_encoding
soup = BeautifulSoup(response.text, "html.parser")

contents['title'] = soup.title.text
contents['h1'] = soup.find('h1').text
postdatetimesoup = soup.find('div', attrs={'id': 'sirius2wp_postdate'})

metadesc = soup.find('meta', attrs={'name': 'description'})
contents['metadesc'] = metadesc['content']

soup.find('div', id='categorylist')

article in soup.find_all('article')
if article.has_attr('class') == False:

atag = soup.find('a')
url = atag['href']

soupの内容出力

soupの内容を出力する方法には、成形されるprettify()と一行のstr()がある。

soupの中身が”<span>ほげほげ</span>”だった場合。

prettify()

str()

タイトルとURLをコピーしました