質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

4回答

1673閲覧

スクレイピングで取得したurlの重複を無くしたい。

maron1220

総合スコア18

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2018/12/18 04:29

スクレイピングにてurlを取得しました。
取得後のurlに重複が生じているので、重複を無くしたいのですが、urlとして残したまま、重複を消したいです。

全体のコード

from bs4 import BeautifulSoup import requests html= requests.get("https://www.springfieldspringfield.co.uk/latest-movie-scripts.php?page=1").text soup=BeautifulSoup(html,"html.parser") links=soup.find_all("a") urls=[] for link in links: url=link.get("href") if "/movie_script.php?movie" in url : urls.append ("https://www.springfieldspringfield.co.uk"+url) for url in urls: print(url)

現状

https://www.springfieldspringfield.co.uk/movie_script.php?movie=cedric-the-entertainer-live-from-the-ville https://www.springfieldspringfield.co.uk/movie_script.php?movie=cedric-the-entertainer-live-from-the-ville https://www.springfieldspringfield.co.uk/movie_script.php?movie=im-brent-morin https://www.springfieldspringfield.co.uk/movie_script.php?movie=im-brent-morin https://www.springfieldspringfield.co.uk/movie_script.php?movie=chelsea-peretti-one-of-the-greats https://www.springfieldspringfield.co.uk/movie_script.php?movie=chelsea-peretti-one-of-the-greats 、、、

と言った形で2つずつ重複したurlがある

求めている結果

https://www.springfieldspringfield.co.uk/movie_script.php?movie=cedric-the-entertainer-live-from-the-ville https://www.springfieldspringfield.co.uk/movie_script.php?movie=im-brent-morin https://www.springfieldspringfield.co.uk/movie_script.php?movie=chelsea-peretti-one-of-the-greats

試したこと
dict.fromkey()の使用

for url in urls: l=[url] l_unique_order = list(dict.fromkeys(l)) print(l_unique_order)

結果

['https://www.springfieldspringfield.co.uk/movie_script.php?movie=cedric-the-entertainer-live-from-the-ville'] ['https://www.springfieldspringfield.co.uk/movie_script.php?movie=cedric-the-entertainer-live-from-the-ville'] ['https://www.springfieldspringfield.co.uk/movie_script.php?movie=im-brent-morin'] ['https://www.springfieldspringfield.co.uk/movie_script.php?movie=im-brent-morin'] ['https://www.springfieldspringfield.co.uk/movie_script.php?movie=chelsea-peretti-one-of-the-greats'] ['https://www.springfieldspringfield.co.uk/movie_script.php?movie=chelsea-peretti-one-of-the-greats']

変わらず、、。

よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答4

0

ベストアンサー

言った形で2つずつ重複したurlがある

常にそのような並びになるのなら、スライスしても良いでしょう。

Python

1urls = urls[::2]

もちろん読み込み時に重複を省く方が効率が良いのは間違いないです。
順序が気にならないなら集合を使うと簡潔でしょう。

順序が気になるのなら、帰属を確認してからリストに追加すれば良いです。
(OrderedSetがサードパーティで公開されているので、それを使うのもアリ。)

Python

1urls = [] 2for link in links: 3 url = link.get("href") 4 if "/movie_script.php?movie" in url: 5 url = "https://www.springfieldspringfield.co.uk" + url 6 if url not in urls: 7 urls.append(url)

条件部は url != urls[-1] でも良いでしょう。

また、continueを上手く使うとネストが減り、コードが読み易くなることを期待できます。

Python

1urls = [] 2for link in links: 3 url = link.get("href") 4 if "/movie_script.php?movie" not in url: 5 continue 6 7 url = "https://www.springfieldspringfield.co.uk" + url 8 if url not in urls: 9 urls.append(url)

投稿2018/12/18 08:01

LouiS0616

総合スコア35658

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

maron1220

2018/12/18 10:25

コードまでつけて頂きありがとうございます!
guest

0

重複しないようにスクレイピング方法を変更した方がいいように思います。

サンプル

python

1from urllib.parse import urljoin 2 3import requests 4from bs4 import BeautifulSoup 5 6url = 'https://www.springfieldspringfield.co.uk/latest-movie-scripts.php' 7 8# CSSセレクタでリンクを抽出 9css = 'table#latest-movie-scripts-table > tr > td:nth-of-type(1) > a' 10 11s = requests.Session() 12 13# ページ番号 14r = s.get(url, params={"page": 1}) 15 16soup = BeautifulSoup(r.content, 'html.parser') 17 18result = [] 19 20for i in soup.select(css): 21 22 # URLを結合 23 result.append(urljoin(url, i.get('href'))) 24 25print(result)

投稿2018/12/18 12:18

barobaro

総合スコア1286

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

maron1220

2018/12/18 12:54

毎回、すごく丁寧に教えてくださり、ありがとうございます!!
guest

0

Python, set型で集合演算(和集合、積集合や部分集合の判定など)
https://note.nkmk.me/python-set/

url_list = list(set(urls))

投稿2018/12/18 05:06

barobaro

総合スコア1286

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

maron1220

2018/12/18 10:21

リンク先までつけて頂きありがとうございます!
guest

0

ggレバいくつか出てくると思いますが、何か特殊なことがありますか?

https://www.lifewithpython.com/2013/11/python-remove-duplicates-from-lists.html
https://note.nkmk.me/python-list-unique-duplicate/

投稿2018/12/18 05:01

t_obara

総合スコア5488

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

maron1220

2018/12/18 10:23

自分の検索能力が低すぎました。 ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問