I want to search for multiple Twitter keywords using selenium.

Asked 2 years ago, Updated 2 years ago, 78 views

Prerequisites/What you want to achieve
python:using selenium
I'd like to read the data repeatedly from Excel and search Twitter.
If there are more than two cities in column C, I would like to search on Twitter as follows.

Excel data

Order of search

Los Angeles, New York, California, USA → What do you want to search for in the first loop?
Tokyo, Japan, Kyoto → What you want to search for in the second loop
Sydney, Australia → What do you want to search for in the third loop?

If there are more than two cities, I would like to separate them with commas and search as follows.

#Enter in containing all of the following keywords in column b:
    # Example US Input
    country=driver.find_element_by_name("allOfTheseWords")
    country.send_keys(b)
    
    # Wait 2 seconds
    time.sleep(2)

    # Enter column c of the city to include the entire following keywords:
    # Example Los Angeles Input
    city=driver.find_element_by_name("thisExactPhrase")
    city.send_keys(c)

    # If there are two or more columns of city c
    # Enter the city c column to include one of the following keywords:
    # New York Input
    if list>list[1]:
     city=driver.find_element_by_name("anyOfTheseWords")
     city.send_keys (list[1])

    # If there are more than three columns of city c
    # Enter the city c column into without the following keywords:
    # California Input
    if list>list[2]:
     city=driver.find_element_by_name("noneOfTheseWords")
     city.send_keys (list[2])

I am currently searching on Twitter as follows.
③TypeError on the .Also, Los Angeles, New York, California all cities are entered in 2.
I'd like to enter only Los Angeles separated by commas.

 次Include all of the following keywords:
US input

②Enter the , which contains the following entire keywords:
Los Angeles, New York, California 

③Enter the with one of the following keywords:
error
if list>list[1]:
TypeError: '>' not supported between instances of 'list' and 'str

Full Code

#Library load for Excel
import openpyxl
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.select import Select  
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys

# Open Excel File
v_wb=openpyxl.load_workbook("twitter.xlsx")

# Active Sheets to Variables
v_ws=v_wb.active
# Loading a Sheet
ws=v_wb.worksheets[0]

# Open your browser. # options=optionbackground 
driver=webdriver.Chrome(executable_path=ChromeDriverManager().install())

# TWITTER URL
URL="https://twitter.com/search-advanced?lang=jp"

# Open Google's top search screen.
driver.get (URL)


# loop from line 2
for i in range(2,v_ws.max_row+1):

    # read column b of country
    b=v_ws['b'+str(i)].value

    # read column c of cities
    c=v_ws['c'+str(i)].value
    print(b)

    # Comma-separated list of characters for cities
    list = c.split(', ')
    print(list)

    # Wait 2 seconds
    time.sleep(2)

    # Enter in containing all of the following keywords in column b:
    # Example US Input
    country=driver.find_element_by_name("allOfTheseWords")
    country.send_keys(b)
    
    # Wait 2 seconds
    time.sleep(2)

    # Enter column c of the city to include the entire following keywords:
    # Example Los Angeles Input
    city=driver.find_element_by_name("thisExactPhrase")
    city.send_keys(c)

    # If there are two or more columns of city c
    # Enter the city c column to include one of the following keywords:
    # New York Input
    if list>list[1]:
     city=driver.find_element_by_name("anyOfTheseWords")
     city.send_keys (list[1])

    # If there are more than three columns of city c
    # Enter the city c column into without the following keywords:
    # California Input
    if list>list[2]:
     city=driver.find_element_by_name("noneOfTheseWords")
     city.send_keys (list[2])

    # Search
    time.sleep(2)
    city.send_keys (Keys.ENTER)
# Repeated -------------------------------------------------------------------------------

Additional information (e.g., Python/Tool version)
I have Python 3.9.5/Windows 10.

If anyone understands, I would appreciate it if you could let me know.
Thank you for your cooperation.

python3 selenium selenium-webdriver

2022-09-30 10:52

1 Answers

I was able to get the elements of the list and realize what I wanted to do.
Check the number of elements in the list and enter them on Twitter if the size is greater than or equal to zero.

Reference URL: Two ways to determine if the Python list is empty

iflen(list)>0:
 print ("If greater than list 0 element")
 # Enter column b of the city to include the entire following keywords:
 city=driver.find_element_by_name("thisExactPhrase")
 city.send_keys (list[0])

iflen(list)>1:
 print ("If greater than list 1 element")
 # If there is more than one city in row b
 # Enter column b of the city to contain one of the following keywords:
 city=driver.find_element_by_name("anyOfTheseWords")
 city.send_keys (list[1])


2022-09-30 10:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.