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

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

Beautiful Soup模板介紹與安裝

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

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黔西| 冷水江市| 大英县| 洱源县| 松潘县| 高台县| 辛集市| 德阳市| 苏尼特右旗| 韶关市| 稷山县| 永嘉县| 襄城县| 柘城县| 宁蒗| 涡阳县| 个旧市| 永德县| 宿松县| 郯城县| 资中县| 鹤峰县| 丹巴县| 周口市| 沂源县| 台东市| 泸定县| 靖边县| 加查县| 新田县| 襄汾县| 石楼县| 邵阳县| 翁牛特旗| 正宁县| 东丰县| 钦州市| 会宁县| 宁武县| 临海市| 信丰县|