欧美三区_成人在线免费观看视频_欧美极品少妇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條評論
主站蜘蛛池模板: 婷婷开心六月久久综合丁香 | 日本黄 色 成 年 人免费观看 | 精品日韩 | 日日操网站 | 亚洲精品视频久久 | 日韩精品小视频 | 久久久久久中文字幕 | 91久久青青草原免费 | 久久99精品久久 | 欧美人xx| 日韩精品网 | 中文字幕在线观看视频一区 | 欧美性一区二区三区 | 亚洲欧洲日产国码在线观看 | 日韩精品一区二区三区国语自制 | 天堂动漫 | 国产日韩精品视频 | 色插综合| 亚洲一区在线观 | 婷婷色国产偷v国产偷v小说 | 久久久久久中文字幕 | 久久精品国产999大香线焦 | 亚拍自拍 | 男女全黄做爰视频免费看 | 黄色片视频在线观看 | 亚洲免费视频在线观看 | 亚洲视频 中文字幕 | 免费看毛片网 | 波多野结衣综合 | 在线视频97| 日本一级高清不卡视频在线 | 伊人精品国产 | 2019中文字幕视频 | 欧美午夜伦理片 | 一区二区中文 | 成年在线视频免费视频观看 | 欧美777精品久久久久网 | 日本高清免费不卡在线播放 | 久久综合色播 | 亚洲精品一区二区三区蜜桃久 | 国产深夜福利视频在线 |