我試圖在新聞中抓取回復。在使用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
感謝您的閱讀。
永遠,永遠,永遠,永遠使用'urllib'時,你可以使用'requests'代替。 –
@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) 但無效。我可以看到相同的錯誤 –
不,我的意思是你正在使用'urllib'庫而不是'requests'庫進行請求。 'request'只是更容易處理。做'html = requests.get(url)'。 –