【发布时间】:2019-10-16 23:28:34
【问题描述】:
我正在使用 Python(3.7) 进行一个项目,在该项目中我需要抓取标题和网址的前几个 Google 结果,我使用 BeautifulSoup 进行了尝试,但它不起作用:
这是我尝试过的:
import requests
from my_fake_useragent import UserAgent
from bs4 import BeautifulSoup
ua = UserAgent()
google_url = "https://www.google.com/search?q=python" + "&num=" + str(5)
response = requests.get(google_url, {"User-Agent": ua.random})
soup = BeautifulSoup(response.text, "html.parser")
result_div = soup.find_all('div', attrs={'class': 'g'})
links = []
titles = []
descriptions = []
for r in result_div:
# Checks if each element is present, else, raise exception
try:
link = r.find('a', href=True)
title = r.find('h3', attrs={'class': 'r'}).get_text()
description = r.find('span', attrs={'class': 'st'}).get_text()
# Check to make sure everything is present before appending
if link != '' and title != '' and description != '':
links.append(link['href'])
titles.append(title)
descriptions.append(description)
# Next loop if one element is not present
except:
continue
print(titles)
但它不返回任何东西。
当我尝试像这样获取HTML 时:
url = 'https://google.com/search?q=python'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'lxml')
print(soup.prettify())
这是它返回的内容:(添加了一个返回的 HTML 代码示例)
<div id="main">
<div class="ZINbbc xpd O9g5cc uUPGi">
<div>
<div class="jfp3ef">
<a href="/url?q=https://www.python.org/&sa=U&ved=2ahUKEwiCrK7AvsXiAhWxq1kKHTknCuoQFjAAegQIBxAB&usg=AOvVaw0nCy-teBd7nOrThY5YGQ4o">
<div class="BNeawe vvjwJb AP7Wnd">
Python.org
</div>
<div class="BNeawe UPmit AP7Wnd">
https://www.python.org
</div>
</a>
</div>
<div class="NJM3tb">
</div>
<div class="jfp3ef">
<div>
<div class="BNeawe s3v9rd AP7Wnd">
<div>
<div>
<div class="Ap5OSd">
<div class="BNeawe s3v9rd AP7Wnd">
The official home of the Python Programming Language.
</div>
</div>
<div class="v9i61e">
<div class="BNeawe s3v9rd AP7Wnd">
<span class="BNeawe">
<a href="/url?q=https://www.python.org/downloads/&sa=U&ved=2ahUKEwiCrK7AvsXiAhWxq1kKHTknCuoQjBAwAXoECAcQAw&usg=AOvVaw0TKe6ApGOQcWuHcXIkvAT0">
<span class="XLloXe AP7Wnd">
Download Python
</span>
</a>
</span>
</div>
</div>
<div class="v9i61e">
<div class="BNeawe s3v9rd AP7Wnd">
<span class="BNeawe">
<a href="/url?q=https://www.python.org/about/gettingstarted/&sa=U&ved=2ahUKEwiCrK7AvsXiAhWxq1kKHTknCuoQjBAwAnoECAcQBQ&usg=AOvVaw03o9Qt-KFSbwECm8-wmUZS">
<span class="XLloXe AP7Wnd">
Python For Beginners
</span>
</a>
</span>
</div>
</div>
<div class="v9i61e">
<div class="BNeawe s3v9rd AP7Wnd">
<span class="BNeawe">
<a href="/url?q=https://www.python.org/doc/&sa=U&ved=2ahUKEwiCrK7AvsXiAhWxq1kKHTknCuoQjBAwA3oECAcQBw&usg=AOvVaw3Yz3mO8HXGJoaf35qhyb3V">
<span class="XLloXe AP7Wnd">
Documentation
</span>
</a>
</span>
</div>
</div>
<div class="v9i61e">
<div class="BNeawe s3v9rd AP7Wnd">
<span class="BNeawe">
<a href="/url?q=https://docs.python.org/&sa=U&ved=2ahUKEwiCrK7AvsXiAhWxq1kKHTknCuoQjBAwBHoECAcQCQ&usg=AOvVaw0nY6NyZm0wErJJ1RIgTiPm">
<span class="XLloXe AP7Wnd">
Python Docs
</span>
</a>
</span>
</div>
</div>
<div class="v9i61e">
<div class="BNeawe s3v9rd AP7Wnd">
<span class="BNeawe">
<a href="/url?q=https://www.python.org/psf/&sa=U&ved=2ahUKEwiCrK7AvsXiAhWxq1kKHTknCuoQjBAwBXoECAcQCw&usg=AOvVaw3HoEDHmdRBcufXuwakPCAz">
<span class="XLloXe AP7Wnd">
Python Software Foundation
</span>
</a>
</span>
</div>
</div>
<div>
<div class="BNeawe s3v9rd AP7Wnd">
<span class="BNeawe">
<a href="/url?q=https://www.python.org/downloads/release/python-373/&sa=U&ved=2ahUKEwiCrK7AvsXiAhWxq1kKHTknCuoQjBAwBnoECAcQDQ&usg=AOvVaw3HsJpvpsCvYikd_mP7ndN3">
<span class="XLloXe AP7Wnd">
Python 3.7.3
</span>
</a>
</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
【问题讨论】:
-
不要做
except: continue,每次在你压制的循环中可能会发生一个简单的异常 -
我在try块外也试过了,还是返回空列表。
-
它为我返回
['Download Python']。它不会为您打印出来? -
是的,对我来说它只返回
[]。
标签: python html web-scraping beautifulsoup python-beautifultable