|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- """世界大学排名"""
- import requests
- from bs4 import BeautifulSoup
- import bs4
- def getHTMLText(url):
- try:
- r=requests.get(url,timeout=30)
- r.raise_for_status()
- r.encoding=r.apparent_encoding
- return r.text
- except:
- return ""
- def fillMovieList(ulist,html):
- soup=BeautifulSoup(html,"html.parser")
- for div in soup.find('body').children:
- if isinstance(div,bs4.element.Tag):
- td=('p')
- ulist.append([td[0].string])
- else:
- return ""
- def printMovieList(ulist,num):
- tplt="{:^10}\t"
- print(tplt.format("大学名称"))
- def main():
- uinfo=[]
- url='http://www.unjs.com/z/1646082.html'
- html=getHTMLText(url)
- fillMovieList(uinfo,html)
- printMovieList(uinfo,10)
- main()
复制代码
运行显示:
大学名称
Process finished with exit code 0
我想知道为什么没有爬取到我想要的大学排名信息呢?是我的解析标签的问题吗?求助大神!
是这样吧?
- """世界大学排名"""
- import requests
- from bs4 import BeautifulSoup
- import bs4
- def getHTMLText(url):
- try:
- r = requests.get(url, timeout=30, headers={"User-Agent": "Mozilla/5.0"})
- r.raise_for_status()
- r.encoding = r.apparent_encoding
- return r.text
- except Exception:
- return ""
- def fillMovieList(ulist, html):
- soup = BeautifulSoup(html, "html.parser")
- for div in soup.find('div', class_="content"):
- if isinstance(div, bs4.element.Tag):
- ulist.append(div.string)
- else:
- return ""
- def printMovieList(ulist, num):
- tplt = "{:^10}\t"
- print(tplt.format("大学名称"))
- for i in range(num):
- u = ulist[i]
- print(tplt.format(u))
- def main():
- uinfo = []
- url = 'http://www.unjs.com/z/1646082.html'
- html = getHTMLText(url)
- fillMovieList(uinfo, html)
- printMovieList(uinfo, 10)
- main()
复制代码
|
|