Python Forum
Using Excel Cell As A Variable In A Loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Excel Cell As A Variable In A Loop
#7
Hi Snippsat,

A big thanks for your help and direction too. I spent time to learn from and apply what you suggested, which included fixing up some problems with my initial code, such as the defining the variable's value at the top for starters.

I then used your code to test opening an Excel file and refer it to a specific column and it worked beautifully. Smile After trying with your example, I also tried my own URLs in Excel and it also referenced those perfectly too.

One of the key takeaway's I learnt from your code was how to reference a column into a loop.

For example:

]for cell in ws['C']:
    url = cell.value
Thank you for teaching me how to do this- really appreciate your time.



(Jul-10-2021, 11:58 AM)snippsat Wrote: There are serval problem or missing stuff with your fist code.
Start with for page in current_url: there is no current_url reference to loop over?
You read from a Excel file that have url list,so look like first loop is no needed at all.
Line 18,19,20 need to inside loop block.

Here is basic test if have urls in a Excel file and iterate over column A.
import openpyxl

wb = openpyxl.load_workbook('url.xlsx')
ws = wb['url_info']
for cell in ws['A']:
    print(cell.value)
Output:
https://python-forum.io/ https://www.google.no/ https://edition.cnn.com/
So if want open urls in BS it would be like this.
import openpyxl
from bs4 import BeautifulSoup
import requests

wb = openpyxl.load_workbook('url.xlsx')
ws = wb['url_info']
for cell in ws['A']:
    print(cell.value)
    response = requests.get(cell.value)
    soup = BeautifulSoup(response.content, 'lxml')
    print(soup.find('title'))
Output:
https://python-forum.io/ <title>Python Forum</title> https://www.google.no/ <title>Google</title> https://edition.cnn.com/ <title>CNN International - Breaking News, US News, World News and Video</title>
Reply


Messages In This Thread
RE: Using Excel Cell As A Variable In A Loop - by knight2000 - Jul-18-2021, 10:45 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable definitions inside loop / could be better? gugarciap 2 468 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,838 Nov-07-2023, 09:49 AM
Last Post: buran
  Problem with print variable in print.cell (fpdf) muconi 0 681 Dec-25-2022, 02:24 PM
Last Post: muconi
  How to loop through all excel files and sheets in folder jadelola 1 4,552 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  Deleting rows based on cell value in Excel azizrasul 11 2,709 Oct-19-2022, 02:38 AM
Last Post: azizrasul
  export into excel, how to implement pandas into for-loop deneme2 6 2,529 Sep-01-2022, 05:44 AM
Last Post: deneme2
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,623 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  loop (create variable where name is dependent on another variable) brianhclo 1 1,160 Aug-05-2022, 07:46 AM
Last Post: bowlofred
  Multiple Loop Statements in a Variable Dexty 1 1,220 May-23-2022, 08:53 AM
Last Post: bowlofred
Big Grin Variable flag vs code outside of for loop?(Disregard) cubangt 2 1,198 Mar-16-2022, 08:54 PM
Last Post: cubangt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020