0

i tried to scraping reply in news.

i tried tried many time.

but i can see only Traceback.

please help me.

i wrote code like this:

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")

when i run this : bs.find('span',class="u_cbox_contents")

i can see only many error

error is this.

SyntaxError: invalid syntax

how to i fix code to run well??

please help me.

i run this python 3.4.4 version, windows 8.1 64x

thanks for reading.

4

1 回答 1

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 版本

于 2016-06-30T04:26:23.807 回答