티스토리 뷰

SWDesk

[Python] 웹 정보 수집하기(1)

bizmaker 2020. 12. 26. 09:55

 

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from bs4 import BeautifulSoup

def fGetLinkPage(linkAddress):
    driver = webdriver.Chrome()
    driver.get(linkAddress)
    try:
        title1 = driver.find_element_by_tag_name('title')
        print("[title]", title1.text)
    except :
        print("[Excepted]-TITLE")
        
    try:
        body1 = driver.find_element_by_tag_name('body')
        print("[body]", body1.text)
    except :
        print("[Excepted-BODY]")    
    driver.close()

def fGetLinkPage2(linkAddress):
    driver = webdriver.Chrome()
    driver.get(linkAddress)
    try:
        print(driver.title)
    except :
        print("[Excepted]-TITLE")

    try:
        body1 = driver.find_element_by_tag_name('body')
        print("[body]", body1.text)
    except :
        print("[Excepted-BODY]")    
    driver.close()
    
    
driver = webdriver.Chrome(executable_path=r'C:\Users\Bilient\Documents\PythonProjects\chromedriver.exe')
driver.get("https://www.google.com")
elem = driver.find_element_by_name("q")
elem.send_keys("MM2E")
elem.send_keys(Keys.RETURN)
Items = driver.find_elements_by_css_selector(".yuRUbf")
count = 1
for item1 in Items:
    #print(string(item1))
    link1 = item1.find_element_by_tag_name("a")
    #print(link1.getText())
    print(link1.get_attribute('href'))
    fGetLinkPage2(link1.get_attribute('href'))
driver.close()    

반응형