欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python異步爬取知乎熱榜實例分享

 更新時間:2022年04月11日 20:03:51   作者:程序員班長  
這篇文章主要介紹了Python異步爬取知乎熱榜實例分享,文章圍繞Python異步爬取是我相關(guān)資料展開對知乎熱榜爬取的相關(guān)內(nèi)容,需要的小伙伴卡哇伊參考一下

一、錯誤代碼:摘要和詳細(xì)的url獲取不到

import asyncio
from bs4 import BeautifulSoup
import aiohttp
?
headers={
? ? 'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
? ? 'referer': 'https://www.baidu.com/s?tn=02003390_43_hao_pg&isource=infinity&iname=baidu&itype=web&ie=utf-8&wd=%E7%9F%A5%E4%B9%8E%E7%83%AD%E6%A6%9C'
}
async def getPages(url):
? ? async with aiohttp.ClientSession(headers=headers) as session:
? ? ? ? async with session.get(url) as resp:
? ? ? ? ? ? print(resp.status) ?# 打印狀態(tài)碼
? ? ? ? ? ? html=await resp.text()
? ? soup=BeautifulSoup(html,'lxml')
? ? items=soup.select('.HotList-item')
? ? for item in items:
? ? ? ? title=item.select('.HotList-itemTitle')[0].text
? ? ? ? try:
? ? ? ? ? ? abstract=item.select('.HotList-itemExcerpt')[0].text
? ? ? ? except:
? ? ? ? ? ? abstract='No Abstract'
? ? ? ? hot=item.select('.HotList-itemMetrics')[0].text
? ? ? ? try:
? ? ? ? ? ? img=item.select('.HotList-itemImgContainer img')['src']
? ? ? ? except:
? ? ? ? ? ? img='No Img'
? ? ? ? print("{}\n{}\n{}".format(title,abstract,img))
?
if __name__ == '__main__':
? ? url='https://www.zhihu.com/billboard'
? ? loop=asyncio.get_event_loop()
? ? loop.run_until_complete(getPages(url))
? ? loop.close()

二、查看JS代碼

發(fā)現(xiàn)詳細(xì)鏈接、圖片鏈接、問題摘要等都在JS里面(CSDN的開發(fā)者助手插件確實好用)

正則表達(dá)式獲取上述信息:

接下來就是詳細(xì)的代碼啦

import asyncio
import json
import re
import aiohttp
?
headers={
? ? 'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
? ? 'referer': 'https://www.baidu.com/s?tn=02003390_43_hao_pg&isource=infinity&iname=baidu&itype=web&ie=utf-8&wd=%E7%9F%A5%E4%B9%8E%E7%83%AD%E6%A6%9C'
}
async def getPages(url):
? ? async with aiohttp.ClientSession(headers=headers) as session:
? ? ? ? async with session.get(url) as resp:
? ? ? ? ? ? print(resp.status) ?# 打印狀態(tài)碼
? ? ? ? ? ? html=await resp.text()
?
? ? regex=re.compile('"hotList":(.*?),"guestFeeds":')
? ? text=regex.search(html).group(1)
? ? # print(json.loads(text)) ? # json換成字典格式
? ? for item in json.loads(text):
? ? ? ? title=item['target']['titleArea']['text']
? ? ? ? question=item['target']['excerptArea']['text']
? ? ? ? hot=item['target']['metricsArea']['text']
? ? ? ? link=item['target']['link']['url']
? ? ? ? img=item['target']['imageArea']['url']
? ? ? ? if not img:
? ? ? ? ? ? img='No Img'
? ? ? ? if not question:
? ? ? ? ? ? question='No Abstract'
? ? ? ? print("Title:{}\nPopular:{}\nQuestion:{}\nLink:{}\nImg:{}".format(title,hot,question,link,img))
?
if __name__ == '__main__':
? ? url='https://www.zhihu.com/billboard'
? ? loop=asyncio.get_event_loop()
? ? loop.run_until_complete(getPages(url))
? ? loop.close()

到此這篇關(guān)于Python異步爬取知乎熱榜實例分享的文章就介紹到這了,更多相關(guān)Python異步爬取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python使用Web框架Flask開發(fā)項目

    Python使用Web框架Flask開發(fā)項目

    本文詳細(xì)講解了Python使用Web框架Flask開發(fā)項目的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • 在python中實現(xiàn)強制關(guān)閉線程的示例

    在python中實現(xiàn)強制關(guān)閉線程的示例

    今天小編就為大家分享一篇在python中實現(xiàn)強制關(guān)閉線程的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • python小程序之飄落的銀杏

    python小程序之飄落的銀杏

    這篇文章主要介紹了利用制作的python小程序-飄落的銀杏,代碼詳細(xì),簡單易懂,有需要練習(xí)python的朋友可以參考下
    2021-04-04
  • python 錄制系統(tǒng)聲音的示例

    python 錄制系統(tǒng)聲音的示例

    這篇文章主要介紹了python 錄制系統(tǒng)聲音的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • 淺談Python3多線程之間的執(zhí)行順序問題

    淺談Python3多線程之間的執(zhí)行順序問題

    這篇文章主要介紹了淺談Python3多線程之間的執(zhí)行順序問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • 如何基于Python實現(xiàn)word文檔重新排版

    如何基于Python實現(xiàn)word文檔重新排版

    這篇文章主要介紹了如何基于Python實現(xiàn)word文檔重新排版,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • Python列表的淺拷貝與深拷貝

    Python列表的淺拷貝與深拷貝

    這篇文章主要介紹了Python列表的淺拷貝與深拷貝,對列表深拷貝就是無論怎樣改動新列表,單維or多維,原列表都不變,需要的小伙伴可以參考下面更詳細(xì)內(nèi)容
    2022-03-03
  • python獲取柵格點和面值的實現(xiàn)

    python獲取柵格點和面值的實現(xiàn)

    這篇文章主要介紹了python獲取柵格點和面值的實現(xiàn),具有很好的參考價值,希望對大家有多幫助。一起跟隨小編過來看看吧
    2020-03-03
  • django filters實現(xiàn)數(shù)據(jù)過濾的示例代碼

    django filters實現(xiàn)數(shù)據(jù)過濾的示例代碼

    這篇文章主要介紹了django filters實現(xiàn)數(shù)據(jù)過濾的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • pydantic?resolve解決嵌套數(shù)據(jù)結(jié)構(gòu)生成痛點分析

    pydantic?resolve解決嵌套數(shù)據(jù)結(jié)構(gòu)生成痛點分析

    這篇文章主要為大家介紹了pydantic?resolve解決嵌套數(shù)據(jù)結(jié)構(gòu)生成痛點分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04

最新評論