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

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

Beautiful Soup模板介紹與安裝

2019-11-10 16:53:49
字體:
來源:轉載
供稿:網友

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 商都县| 秦安县| 阿坝| 城市| 博白县| 宁远县| 商南县| 墨玉县| 岢岚县| 临朐县| 万州区| 色达县| 东光县| 奇台县| 大姚县| 宁津县| 孙吴县| 静海县| 扶绥县| 新晃| 永平县| 崇文区| 镇赉县| 湟中县| 广昌县| 沐川县| 怀柔区| 德庆县| 泰和县| 青铜峡市| 那曲县| 新乡市| 泰宁县| 永安市| 平山县| 灌云县| 定远县| 丰城市| 鄂托克前旗| 宁武县| 海淀区|