国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Beautiful Soup模板介紹與安裝

2019-11-10 17:27:23
字體:
來源:轉載
供稿:網友

Beautiful Soup

-Python第三方庫,用于從HTML或xml中提取數據

-官網: http://www.crummy.com/software/BeautifulSoup/

安裝并測試beautifulsoup4

-安裝:pip install beautifulsoup4

-測試:import bs4

用代碼測試:

import bs4

PRint(bs4)

將文件安裝到Scripts目錄步驟:

cd /位置

dir

pip install beautifulsoup4

安裝成功后的測試結果

<module 'bs4' from 'C://Users//Administrator//AppData//Local//Programs//Python//Python36-32//lib//site-packages//bs4//__init__.py'>

最后來個代碼玩玩

__author__ = 'Mr'import urllib.requestimport reimport timefrom bs4 import BeautifulSoupp = re.compile('/jack_user/article/details/........')#博客主頁url = "http://blog.csdn.net/jack_user" #使用build_opener()是為了讓python程序模仿瀏覽器進行訪問opener = urllib.request.build_opener()opener.addheaders = [('User-agent', 'Mozilla/5.0')]html = opener.open(url).read().decode('utf-8')allfinds = p.findall(html)#print(allfinds)urlBase = "http://blog.csdn.net"#需要將網址合并的部分#頁面中的網址有重復的,需要使用set進行去重復mypages = list(set(allfinds))for i in range(len(mypages)):    mypages[i] = urlBase+mypages[i]print('要刷的網頁有:')for index , page in enumerate(mypages) :    print(str(index), page)#設置每個網頁要訪問的次數brushNum = 200#所有的頁面都訪問print('下面開始刷了哦:')for index , page in enumerate(mypages) :    for j in range(brushNum):        try :            pageContent = opener.open(page).read().decode('utf-8')            #使用BeautifulSoup解析每篇博客的標題            soup = BeautifulSoup(pageContent)            blogTitle = str(soup.title.string)            blogTitle = blogTitle[0:blogTitle.find('-')]            print(str(j) , blogTitle)                     except urllib.error.HTTPError:            print('urllib.error.HTTPError')            time.sleep(3)#出現錯誤,停幾秒先                    except urllib.error.URLError:            print('urllib.error.URLError')            time.sleep(3)#出現錯誤,停幾秒先        time.sleep(0.5)#正常停頓,以免服務器拒絕訪問


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 文昌市| 普格县| 罗江县| 荣成市| 江永县| 云霄县| 砀山县| 汕尾市| 仪征市| 日土县| 渝中区| 华容县| 武功县| 蒲城县| 吕梁市| 濮阳县| 新泰市| 甘谷县| 临泉县| 荔浦县| 自贡市| 贺州市| 南木林县| 辽宁省| 泸州市| 于田县| 合山市| 本溪市| 江津市| 潼南县| 津南区| 黄山市| 兴文县| 鹤庆县| 桃园县| 奉节县| 仪征市| 微山县| 赤壁市| 得荣县| 宜阳县|