跟著書爬一下酷狗音樂top500
我爬取的思路是先尋找所有網(wǎng)頁,然后再請求所有網(wǎng)頁,并將他們的內(nèi)容用beautifulsoup解析出來,最后直接print,但是卻報(bào)錯(cuò)了,我看了一下思路應(yīng)該不會有什么問題???求各位大神幫助,
報(bào)錯(cuò):
No connection adapters were found for '['http://www.kugou.com/yy/rank/...']'
我的代碼如下:
import requests
from bs4 import BeautifulSoup
import time
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0'
}#請求頭
def get_info(url): #獲取網(wǎng)站信息
res = requests.get(url,headers = headers) #請求網(wǎng)頁
soup = BeautifulSoup(res.text,'lxml') #解析數(shù)據(jù)
#名次:
nums = soup.select('.pc_temp_songlist > ul:nth-of-type(1) > li > span:nth-of-type(3) > strong:nth-of-type(1)')
#歌手-名字:
titles = soup.select('.pc_temp_songlist > ul:nth-of-type(1) > li > a:nth-of-type(4)')
#時(shí)間:
times = soup.select('.pc_temp_songlist > ul:nth-of-type(1) > li > span:nth-of-type(5) > span:nth-of-type(4)')
for num,title,time in zip(nums,titles,times):
data = {
'名次':num.get_text().strip(),
'歌手':title.get("title").get_text().split('-')[0],
'名字':prices.get("title").get_text().split('-')[1],
'時(shí)間':address.get_text().strip(),
}
print(data)
time.sleep(2)
主程序
#主程序
urls = ['http://www.kugou.com/yy/rank/home/{}-8888.html?from=rank'.format(number) for number in range(1,24)] #收集1-23頁
for single_url in urls:
get_info(single_url)
time.sleep(5)
主程序直接卡在那里沒有任何信息打出來,于是我就試了一下第一頁的爬取['http://www.kugou.com/yy/rank/home/1-8888.html?from=rank']
,結(jié)果報(bào)錯(cuò)了,很奇怪好像是沒連上的意思,我直接點(diǎn)開網(wǎng)頁是能連上的。
代碼如下:
url = ['http://www.kugou.com/yy/rank/home/1-8888.html?from=rank']
get_info(url)
報(bào)錯(cuò)如下:
No connection adapters were found for '['http://www.kugou.com/yy/rank/home/1-8888.html?from=rank']'
百度了一下這個(gè)報(bào)錯(cuò)試了一下沒轍,而且百度上此報(bào)錯(cuò)內(nèi)容較少 拜托各位!
nums = soup.select('.pc_temp_songlist > ul:nth-of-type(1) > li > span:nth-of-type(3) > strong:nth-of-type(1)')
titles = soup.select('.pc_temp_songlist > ul:nth-of-type(1) > li > a:nth-of-type(4)')
times = soup.select('.pc_temp_songlist > ul:nth-of-type(1) > li > span:nth-of-type(5) > span:nth-of-type(4)')
這個(gè)數(shù)據(jù)解析有問題啊,所以當(dāng)然沒有打印輸出了
你覺得卡住,每次循環(huán)要sleep 7秒,而且輸出為空造成的假象吧
以下代碼供參考:
import requests
from bs4 import BeautifulSoup
url='http://www.kugou.com/yy/rank/...{}-8888.html?from=rank'
def get_info(url):
res=requests.get(url)
soup=BeautifulSoup(res.text,'lxml')
infoes=soup.select('div.pc_temp_songlist ul li ')
for info in infoes:
nums=info.select('span.pc_temp_num')[0].text.strip()
singer,name=info['title'].split('-',1)
times=info.select('span.pc_temp_tips_r span.pc_temp_time')[0].text.strip()
print({'名次':nums,'歌手':singer,'歌名':name,'時(shí)長':times})
if __name__=='__main__':
urls = [url.format(i) for i in range(1, 24)]
for url in urls:
get_info(url)
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。