国产探花免费观看_亚洲丰满少妇自慰呻吟_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)#正常停頓,以免服務器拒絕訪問


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 澎湖县| 新乡市| 河池市| 松原市| 平和县| 化州市| 苏尼特左旗| 方城县| 宁夏| 南安市| 古丈县| 江安县| 灵台县| 巩留县| 博罗县| 赤峰市| 嘉峪关市| 平乐县| 洱源县| 平安县| 三河市| 大方县| 新闻| 德安县| 石渠县| 鄂尔多斯市| 延边| 内江市| 岳池县| 开封县| 福安市| 肥城市| 林口县| 宣化县| 紫阳县| 瑞丽市| 和政县| 德阳市| 墨竹工卡县| 巢湖市| 封开县|