我正在尝试使用requests库和Beauty soup库从google.com查询中检索数据。 我在扯头发。无论我尝试什么版本的代码,我都会出错
这是html代码:
<div class="LGOjhe" data-attrid="wa:/description" aria-level="3" role="heading" data-hveid="CAYQAA">
<span class="ILfuVd NA6bn">
<span class="hgKElc">
Right-click the speaker icon and select "Open Sound Settings." 3. Scroll down to "Input." Windows will show you which <b>microphone</b> is currently your default — in other words, which one it's using right now — and a blue bar showing your volume levels. Try talking into your <b>microphone</b>.
</span>
</span>
我使用以下代码:
page = rq.get('http://www.google.com/search?q=where%20is%20my%20microphone', headers = hd)
soup = BeautifulSoup(page.content, 'html.parser')
#answer = soup.find('span', {'class_': 'hgKElc'}).getText()
answer = soup.find('div', {'class_': 'kJ442'}).getText()
我得到一个错误,none的响应没有getText()
方法。这意味着我的检索方法失败:
File "C:\Users\User\Documents\study\PythonProjectVoice\PythonVoice03.py", line 115, in respond
answer = soup.find('div', {'class_': 'kJ442'}).getText()
AttributeError: 'NoneType' object has no attribute 'getText'
非常感谢您的回答
有不同的地方可以改进,但是发生错误的原因是没有像
kJ442
这样的类的<div>
要获取
<span>
的文本,请使用:示例
相关问题 更多 >
编程相关推荐