在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python  HTML/ 【python小白】 寫簡單爬蟲的時候遇到問題,采集不到想要的標簽

【python小白】 寫簡單爬蟲的時候遇到問題,采集不到想要的標簽

代碼如圖所示:

# -*- coding:utf-8 -*-
import webbrowser
import pyperclip
import sys
import bs4
import requests

search_target = pyperclip.copy("kkkk")
if len(sys.argv) > 1:
     search_target = ' '.join(sys.argv[1:])
else:
    search_target = pyperclip.paste()

res_temp = 'http://www.baidu.com/s?wd=' + search_target
webbrowser.open(res_temp)

res = requests.get(res_temp)
bsObj = bs4.BeautifulSoup(res.text, 'html.parser')
print(bsObj)
#h3 = bsObj.findAll('h3', {'class':'t'}) 
#print(h3)



print("---1---")
div = bsObj.findAll('div', {'class':'result c-container '}) 
print(div)
print("---2---")
h3 = div.findAll('h3',{'class':'t'})
print(h3)
print('---3---')

運行結(jié)果如下:

clipboard.png

等到運行這一行:

h3 = div.findAll('h3',{'class':'t'})

就會出現(xiàn)錯誤。
很奇怪,也就是說,這一段代碼可以采集div這個標簽,卻無法采集h3這個標簽。
而網(wǎng)頁源代碼如下:

clipboard.png
h3本來就是div這個標簽下面的子標簽,那為什么div可以采集到,h3 就無法采集到呢?

回答
編輯回答
鹿惑

因為這里div=bsObj.findAll('div', {'class': 'result c-container'})

>>> type(div)
<class 'bs4.element.ResultSet'>

div是一個類似于 list 類型的對象, 沒有findAll這個方法, 你可以取div其中的某一個進行操作, 比如:

>>> div[0].findAll('h3', {'class': 't'})
[<h3 class="t"><a data-click="{
                        'F':'778317EA',
...
2018年8月16日 07:38