LoginSignup
souwasora
@souwasora (takei souwa)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

python BeautifulSoup a hrefを取得したい

解決したいこと

<a class="rnn-linkText rnn-linkText--black" href="/company/cmi0163052681/nx1_rq0014886090/?fr=cp_s00700&amp;list_disp_no=1&amp;leadtc=n_ichiran_cst_n5_ttl" target="_blank">当社ご契約者様に対するサポート業務★営業、販売、事務</a>
company/○○....

上記だけ取得したい。
解決方法を教えて下さい。

発生している問題・エラー

スクリーンショット 2022-11-12 1.48.58.png

該当するソースコード

import pandas
import datetime
import requests
import time
from bs4 import BeautifulSoup
import csv
import pandas as pd
from gspread_dataframe import set_with_dataframe
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from time import sleep
browser = webdriver.Chrome(ChromeDriverManager().install())
# コンビニ、レジ、スーパー
url = 'https://next.rikunabi.com/area_wp0313100000/jb0101020110/crn1.html'
browser.get(url)
res = requests.get(url)
#parser = 分割的な意味
soup = BeautifulSoup(res.content, 'html.parser',from_encoding='utf-8')
#一覧で取得
box = soup.find_all('ul', attrs={'class': 'rnn-group rnn-group--xm rnn-jobOfferList'})
for b in box:
    #aタグ取得
    b = b.find_all('a', attrs={'class': 'rnn-linkText rnn-linkText--black'})

自分で試したこと

解決策を教えてください

0

1Answer

てきとうにstring で絞り込んではいかがでしょうか

for b in box:
    links = b.find_all('a', attrs={'class': 'rnn-linkText rnn-linkText--black'})
    for link in links:
      if link.string == '当社ご契約者様に対するサポート業務★営業、販売、事務':
        print(link.get('href')) # /company/cmi0163052681/nx1_rq0014886090/?fr=cp_s00700&list_disp_no=1&leadtc=n_ichiran_cst_n5_ttl

0Like

Your answer might help someone💌