2016-06-30 67 views
0

我試圖在新聞中抓取回復。在使用python進行Web抓取時出現錯誤

我試過很多次了。

但我只能看到Traceback。

請幫幫我。

我寫這樣的代碼:

import re 
import urllib.request 
import urllib 
import requests 
from bs4 import BeautifulSoup 

url='http://news.naver.com/main/ranking/read.nhn?mid=etc&sid1=111&rankingType=popular_week&oid=277&aid=0003773756&date=20160622&type=1&rankingSectionId=102&rankingSeq=1&m_view=1' 
html=request.get(url) 
#print(html.text) 
a=html.text 
bs=BeautifulSoup(a,'html.parser') 
print(bs.prettify()) 
bs.find('span',class="u_cbox_contents") 

當我運行此:bs.find( '跨越' 的class = 「u_cbox_contents」)

我只能看到許多錯誤

錯誤是這樣的。

SyntaxError: invalid syntax

如何修復代碼以運行良好?

請幫幫我。

我運行這個python 3.4.4版本,windows 8.1 64x

感謝您的閱讀。

+0

永遠,永遠,永遠,永遠使用'urllib'時,你可以使用'requests'代替。 –

+0

@AkshatMahajan你的意思是試過這段代碼? : 進口重新 進口urllib.request裏 從BS4進口BeautifulSoup URL =「HTTP://news.naver.com/main/ranking/read.nhn中旬=等與SID 1 = 111&rankingType = popular_week&OID = 277&援助= 0003773756&日期= 20160622&type = 1&rankingSectionId = 102&rankingSeq = 1&m_view = 1' html = urllib.request.urlopen(url) 但無效。我可以看到相同的錯誤 –

+1

不,我的意思是你正在使用'urllib'庫而不是'requests'庫進行請求。 'request'只是更容易處理。做'html = requests.get(url)'。 –

回答

3

繼@AkshatMahajan建議,可以使用requests模塊來完成以下操作。另外,您還可以修改最後一行來查找所需的元素。

##import re 
##import urllib.request 
##import urllib 
import requests 
from bs4 import BeautifulSoup 

url='http://news.naver.com/main/ranking/read.nhn?mid=etc&sid1=111&rankingType=popular_week&oid=277&aid=0003773756&date=20160622&type=1&rankingSectionId=102&rankingSeq=1&m_view=1' 
html=requests.get(url) 
#print(html.text) 
a=html.text 
bs=BeautifulSoup(a,'html.parser') 
print(bs.prettify()) 
print(bs.find('span',attrs={"class" : "u_cbox_contents"})) 

感謝@DiogoMartins您指出正確的Python版本以及

+0

你剛剛複製了@akshat在評論中給出的答案嗎? –

+1

@DiogoMartins是的,我把@akshat建議在評論中改爲請求。並且更改了最後一行,因爲原始代碼行'bs.find('span',class =「u_cbox_contents」)'中有無效的語法錯誤。希望這也有助於 – shaojl7

+1

正確的做法是在你的回答中給予@akshat功勞。此外,您的答案的最後一行將導致一個SyntaxError,因爲問題說,他運行在python 3.4 –

相關問題