2017-11-02 2 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

아마도 * your code *에 대한 정확한 해결책을 찾을 수 없습니다. 그러나 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 

추가 정보 : beautifulsouprequests

+0

STEFANI에게 감사드립니다. 예제 스크립트는 내가 필요한 것입니다. – Jun

+0

6 월을 환영합니다. –

+0

nice & clean. 감사! – RobBenz

관련 문제