我有一个简单的问题:
当您使用Beautiful Soup刮取网站的某个部分时,您可以使用data.find()/ data.findAll()或data.select()
现在的问题是..find()和.select()方法之间是否存在显着差异?(例如,在表现或灵活性方面,或......)
还是他们一样?
亲切的问候
我正在使用 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() …Run Code Online (Sandbox Code Playgroud) html python beautifulsoup web-scraping python-beautifultable