Search code examples
pythonweb-scrapingurllibtrackback

error during Web scraping using python


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.


Solution

  • Following @AkshatMahajan advise, the below can be done using requests module instead. In addition, you can also modify the last line to find the desired element.

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

    Thanks to @DiogoMartins for pointing out the correct Python version as well