亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

python利用beautifulSoup實現(xiàn)爬蟲

系統(tǒng) 2052 0

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

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

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

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

上面可以抓取百度搜出來結(jié)果是網(wǎng)球的記錄。

beautifulSoup內(nèi)置了很多非常有用的方法。

幾個比較好用的特性:

構(gòu)造一個node元素

復(fù)制代碼 代碼如下:

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

屬性可以使用attr拿到,結(jié)果是字典

復(fù)制代碼 代碼如下:

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.構(gòu)建一份文檔

            
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'
          

更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 奇米影视777888 | 深夜在线免费观看 | 香蕉爱爱视频 | 黄页在线免费观看 | 一级a俄罗斯毛片免费 | 欧美黑人巨大3dvideo | 色狠狠xx | 爽爽影院在线免费观看 | 国内精品久久久久久久星辰影视 | 男人的天堂在线精品视频 | 老司机永久免费视频 | 久久草在线免费 | 国产高清免费视频 | 久久精品一区二区 | 欧美精品一区二区三区观 | 中文字幕亚洲一区二区v@在线 | 五月婷婷中文字幕 | 国产成人精品一区二区 | 欧美亚洲国产精品久久 | 久久精品94精品久久精品 | 亚洲欧美精品久久 | 国产精品福利在线观看 | 欧美xo影院 | 日韩精品欧美精品中文精品 | 亚洲欧美精品一区二区 | 黄色片免费在线观看视频 | 久久久午夜精品 | 福利一区在线视频 | 国产成人精品一区二区三在线观看 | 欧美日本在线一区二区三区 | 成 人 黄 色 免费网 | 在线aa | 国产一区二区在线视频 | 亚洲综合色丁香麻豆 | 狠狠视频| 日日夜夜中文字幕 | 性久久久久 | 亚洲精品二区 | 中文字幕最新中文字幕中文字幕 | 国产精品欧美亚洲区 | 亚洲欧美韩国 |