2017-11-02 19 views
-7

CSVファイルに保存されているURLのリストを削り取る必要があります。Beautiful Soup&Pythonを使用してCSVから複数のURLを削る

https://stackoverflow.com;code site; 
https://steemit.com;block chain social site; 

は、次のコードは動作します:

私のようなあなたのurls.csvファイルが見えると仮定すると、美しいスープ

+0

お試しください:) – yash

+0

*あなたのコード*の正確な解決策は見つかりません。しかし、CSVを読み、HTTPリクエストを作成し、PythonでHTMLを解析するための多くのドキュメントがあります。あなたのコードが動作する特定の方法ではなく、それに焦点を当ててください。 –

+0

あなたが何かを検索したと言いたいのであれば、何が動作していないかを示してください。その結果、可能な限り重複しないように質問します。 –

答えて

1

に非常に新しいです

#!/usr/bin/python 
# -*- coding: utf-8 -*- 

from bs4 import BeautifulSoup #required to parse html 
import requests #required to make request 

#read file 
with open('urls.csv','r') as f: 
    csv_raw_cont=f.read() 

#split by line 
split_csv=csv_raw_cont.split('\n') 

#remove empty line 
split_csv.remove('') 

#specify separator 
separator=";" 

#iterate over each line 
for each in split_csv: 

    #specify the row index 
    url_row_index=0 #in our csv example file the url is the first row so we set 0 

    #get the url 
    url = each.split(separator)[url_row_index] 

    #fetch content from server 
    html=requests.get(url).content 

    #soup fetched content 
    soup= BeautifulSoup(html) 

    #show title from soup 
    print soup.title.string 

結果:

Stack Overflow - Where Developers Learn, Share, & Build Careers 
Steemit 

その他の情報:beautifulsoupおよびrequests

+0

STEFANIに感謝します。あなたのサンプルスクリプトは、私が必要としていたものです。 – Jun

+0

あなたは大歓迎です! –

+0

nice&clean。ありがとう! – RobBenz

関連する問題