日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python利用beautifulSoup實(shí)現(xiàn)爬蟲(chóng)

系統(tǒng) 2278 0

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

利用 beautifulSoup(文檔 :http://www.crummy.com/software/BeautifulSoup/bs4/doc/)這個(gè)python模塊,可以很輕松的抓取網(wǎng)頁(yè)內(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')
          

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

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

幾個(gè)比較好用的特性:

構(gòu)造一個(gè)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號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 外汇| 大理市| 青冈县| 龙胜| 察哈| 宁乡县| 齐河县| 平塘县| 大邑县| 荣成市| 金门县| 张家口市| 刚察县| 青铜峡市| 婺源县| 文登市| 保靖县| 漳平市| 水城县| 科技| 济南市| 南郑县| 沁水县| 耒阳市| 巨野县| 始兴县| 潼南县| 贵德县| 襄垣县| 伊通| 紫阳县| 息烽县| 清丰县| 平舆县| 固镇县| 井陉县| 江油市| 福鼎市| 荔波县| 新郑市| 南投市|