欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

python利用beautifulSoup實現爬蟲

系統 2120 0

以前講過利用phantomjs做爬蟲抓網頁 //www.jb51.net/article/55789.htm 是配合選擇器做的

利用 beautifulSoup(文檔 :http://www.crummy.com/software/BeautifulSoup/bs4/doc/)這個python模塊,可以很輕松的抓取網頁內容

            
# coding=utf-8
import urllib
from bs4 import BeautifulSoup

url ='http://www.baidu.com/s'
values ={'wd':'網球'}
encoded_param = urllib.urlencode(values)
full_url = url +'?'+ encoded_param
response = urllib.urlopen(full_url)
soup =BeautifulSoup(response)
alinks = soup.find_all('a')
          

上面可以抓取百度搜出來結果是網球的記錄。

beautifulSoup內置了很多非常有用的方法。

幾個比較好用的特性:

構造一個node元素

復制代碼 代碼如下:

soup = BeautifulSoup(' Extremely bold ')
tag = soup.b
type(tag)
#

屬性可以使用attr拿到,結果是字典

復制代碼 代碼如下:

tag.attrs
# {u'class': u'boldest'}

或者直接tag.class取屬性也可。

也可以自由操作屬性

            
tag['class'] = 'verybold'
tag['id'] = 1
tag
# 
            
Extremely bold
del tag['class'] del tag['id'] tag #
Extremely bold
tag['class'] # KeyError: 'class' print(tag.get('class')) # None

還可以隨便操作,查找dom元素,比如下面的例子

1.構建一份文檔

            
html_doc = """

            
              The Dormouse's story
            
            

The Dormouse's story

Once upon a time there were three little sisters; and their names were Elsie , Lacie and Tillie ; and they lived at the bottom of a well.

...

""" from bs4 import BeautifulSoup soup = BeautifulSoup(html_doc)

2.各種搞

            
soup.head
# 
            
              The Dormouse's story
            
            
soup.title
# 
            
soup.body.b
# 
            
              The Dormouse's story
            
            
soup.a
# 
            
              Elsie
            
            
soup.find_all('a')
# [
            
              Elsie
            
            ,
# 
            
              Lacie
            
            ,
# 
            
              Tillie
            
            ]
head_tag = soup.head
head_tag
# 
            
              The Dormouse's story
            
            

head_tag.contents
[
            ]

title_tag = head_tag.contents[0]
title_tag
# 
            
title_tag.contents
# [u'The Dormouse's story']
len(soup.contents)
# 1
soup.contents[0].name
# u'html'
text = title_tag.contents[0]
text.contents

for child in title_tag.children:
  print(child)
head_tag.contents
# [
            ]
for child in head_tag.descendants:
  print(child)
# 
            
# The Dormouse's story

len(list(soup.children))
# 1
len(list(soup.descendants))
# 25
title_tag.string
# u'The Dormouse's story'
          

更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 波多野结衣在线看片 | 亚洲欧美日韩中文字幕久久 | 久久久久国产成人精品亚洲午夜 | 丁香久久| 免费一看一级毛片 | 午夜视频直播 | 激情五月综合婷婷 | 日韩在线观看视频免费 | www.日韩在线 | 亚洲午夜精品一区二区 | 亚洲欧美日韩在线一区二区三区 | 欧美第一色 | 激情网址在线观看 | 亚洲精品乱码久久久久久 | 精品久久洲久久久久护士免费 | 久久久久亚洲 | 91视频苹果版| 草草草在线观看 | 三级福利视频 | 日韩一区二区中文字幕 | 亚洲精品自产拍在线观看app | 黑人巨大videosjapan高清 婷婷在线免费观看 | 一本色道久久88加勒比—综合 | 欧美日韩后 | 国产精品欧美日韩 | 亚洲午夜久久久久中文字幕久 | 一级做a爱片久久 | 久久在线免费视频 | 夜夜操天天操 | 99久在线观看 | 久久亚洲国产精品 | 欧美激情视频一区二区免费 | 欧美一区在线观看视频 | 欧美日本一 | 日韩精品亚洲专区在线电影不卡 | 国产91亚洲精品 | 成人18视频在线 | 国产夜色福利院在线观看免费 | 青青草99热久久 | 国产亚洲精品一区二区 | 三级欧美日韩 |