我试图抓新闻中的答复。
我尝试了很多次。
但我只能看到回溯。
请帮我。
我写了这样的代码:
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('span',class =“ u_cbox_contents”)
我只能看到很多错误
错误是这样。
SyntaxError:语法无效
我如何修复代码以使其正常运行?
请帮我。
我运行此python 3.4.4版本,Windows 8.1 64x
谢谢阅读。
按照@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版本
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句