3 回答
TA贡献1780条经验 获得超5个赞
取决于你想用块做什么,如果只有在href之后使用并使用后代组合select器指定具有子元素的父类和属性href
import requests
from bs4 import BeautifulSoup
ticker='FAST'
url = "https://www.google.com/search?q=nasdaq+%s+earnings+reaction+history&tbs=qdr:m"%(ticker)
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
cache = [searchs['href'] for searchs in soup.select('div.kCrYT [href]')]
print(''.join(cache))
TA贡献1911条经验 获得超7个赞
使用下面的代码从TypeError.
try:
cache.append(searchs.find('a')['href'])
except TypeError:
continue
TA贡献1804条经验 获得超8个赞
不要试图在一行中放这么多:
...
for searchs in soup.find_all('div'):
tag = searchs.find('a')
#print(searchs)
if not tag:
continue
try:
h = tag['href']
except KeyError as e:
continue
print(h)
添加回答
举报