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

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

Beautiful Soup模板介紹與安裝

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

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)#正常停頓,以免服務器拒絕訪問


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西畴县| 万源市| 汉阴县| 枣庄市| 吴旗县| 丹东市| 陈巴尔虎旗| 崇信县| 周口市| 秦皇岛市| 昭通市| 平定县| 泰顺县| 闽清县| 湖口县| 阿鲁科尔沁旗| 离岛区| 乌什县| 富宁县| 吉林省| 青河县| 遂平县| 彰化县| 天水市| 金乡县| 莎车县| 广平县| 灌南县| 建水县| 德钦县| 嘉义县| 平凉市| 苍梧县| 文登市| 铜陵市| 迁安市| 宜阳县| 余姚市| 龙州县| 同德县| 涞水县|