简体   繁体   中英

How do you scrape several websites and have one output on the same line with Python using Selenium?

I wanted to learn Python so I started with learning web scraping. I know my code is all over the place but as I learn more I will clean up and make it more efficient. However, I want to know how to print from several different websites and get an output on the same line.

Example: output: output: output: output: output

Here is my redacted code:

from selenium import webdriver

def bond(x):
    driver = webdriver.Chrome()
    url = 'website'
    driver.get(url)
    year_10_bond = driver.find_elements_by_xpath('element')[0].text
    print(year_10_bond)
    driver.close()

b = bond(print)

def stocks(s):
    driver = webdriver.Chrome()
    for i in range(0, 7661):
        page_num = ('&r=' + str(i * 20 + 1))
        url = 'website)
        driver.get(url)
        tickers = driver.find_elements_by_class_name('element')
        company = driver.find_elements_by_xpath('element')
        price = driver.find_elements_by_xpath('element')
        num_of_tickers = len(tickers)
        for i in range(num_of_tickers):
            print(tickers[i].text + " : " + company[i].text + " : " + price[i].text)

s = stocks(print)

def outstanding(o)
    driver = webdriver.Chrome()
    for i in range(0, 7661):
        page_num = ('&r=' + str(i * 20 + 1))
        url = ('element')
        driver.get(url)
        shares_outstanding = driver.find_elements_by_xpath('element')
        num_of_tickers = len(shares_outstanding)
        for i in range(num_of_tickers):
            print(shares_outstanding[i].text)

o = outstanding(print)

driver.close()

Here is the solution that worked for me I was just putting this in the wrong place at first:

end = ' '

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM