Answer a question

from selenium import webdriver
from bs4 import BeautifulSoup as Bs
import time

driver = webdriver.Chrome(executable_path=r'C:\Users\kaka\PycharmProjects\chromedriver.exe')

google_get = driver.get('https://www.google.com/?q=nlp techniques')
google_search = driver.find_element_by_xpath(
    '/html/body/div[1]/div[3]/form/div[2]/div[1]/div[3]/center/input[1]').click()
time.sleep(1)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)
soup = Bs(driver.page_source, 'html.parser')
time.sleep(1)
link = soup.find_all('div', {'class': 'yuRUbf'})
for lnk in link:
    print(lnk)

I want to grab all the 'href' attribute from the given list. how can i extract href from the given code.

Answers

Since the div & a tag are next to each otherdiv.a was possible.

link = [div.a['href'] for div in 
        soup.find_all('div', attrs={'class' : 'yuRUbf'})]
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐