![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#131 |
|
Hobbyist Programmer
Join Date: May 2006
Posts: 127
Rep Power: 3
![]() |
thanks much, I can almost taste freedom.
|
|
|
|
|
|
#132 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
|
|
|
|
|
|
|
#133 |
|
Hobbyist Programmer
Join Date: May 2006
Posts: 127
Rep Power: 3
![]() |
Sorry little confused regarding the above post, do I know how to tell python to output a blank cell, I can't just write that can I?
highlights = soup.fetchText(re.compile("Highlights"))[0]
if highlights = [0]
financialhighlights = highlights.findParent("table")
else highlights == []
_________ don't know what goes here (if anything?) can I leave
it blank and it will be blank or do I have to
instruct python what to do if it equals []?Well I tested the above on a company link that doesn't have any financial information and it gave me a syntax error, first it highlighted the first equal sign in the line if highlights = [0] Okay I'm looking at my code again and I don't think I'm remotely close to what it needs to be, I'm gonna take another look and try and revise it. hm, perhaps: #Financial Highlights
highlights = soup.fetchText(re.compile("Highlights"))
if highlights == []:
print highlights
elif highlights == [0]:
financialhighlights = highlights.findParent("table")
print financialhighlightsLast edited by zem52887; May 23rd, 2006 at 12:20 PM. |
|
|
|
|
|
#134 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Use "else" instead of "elif ...". Lookup what if and else mean.
With regards to blank cells, I'll give you a quick runthrough of writing information to a CSV file. An example CSV file looks like this: Last name, First name, Phone number Smith, John, 555-1000 Jones, Ann, 555-2000 Wayne, Fred, 555-3000 The above CSV file uses commas to break up the columns. Excel allows you to specify other characters to split the data up (The | character is good, since it's rarely used in text). That's all well and good, but we now need to get Python to create such a file. The following code opens a file and writes some text to it: file = open("C:\Path\To\file.txt", "w")
file.write("Hello World\n")
file.close()Another useful thing to know is that you can join up a list into a string quite easily: x = ["Smith", "John", "555-1000"] print "|".join(x) Smith|John|555-1000 file = open("C:\Path\To\file.txt", "w")
x = ["Smith", "John", "555-1000"]
file.write("|".join(x))
file.close()firstname = "John" lastname = "Smith" phonenumber = "555-1000" spreadsheet_row = [lastname, firstname, phonenumber] |
|
|
|
|
|
#135 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
[0] on its own is a list with zero inside it; it's only an index when it's placed after a variable, like list[0]. As I said, use "len" instead for this sort of thing, or learn the value of the "else" statement. "len" is a very simple function. All it does is to return the size of a list. If a list has three items in it, len(list) returns 3. If a list has 1 item in it, len(list) returns 1. If a list is empty, len(list) returns 0. |
|
|
|
|
|
|
#136 |
|
Hobbyist Programmer
Join Date: May 2006
Posts: 127
Rep Power: 3
![]() |
I'm sorry I'm having a bit of trouble implementing this, but I think I have it now:
highlights = soup.fetchText(re.compile("Highlights"))
z = len(highlights)
if z == [1]:
financialhighlights = highlights.findParent("table")
print financialhighlights
else:
print N/Amy question do I want the script to return these statements or print them? Bah, I tried it with a company with financial highlights and it printed N/A, so I guess I don't have it. |
|
|
|
|
|
#137 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Nearly. Remember that len() returns a single number, not a list:
if len(highlights) == 1: |
|
|
|
|
|
#138 |
|
Hobbyist Programmer
Join Date: May 2006
Posts: 127
Rep Power: 3
![]() |
boo you beat me to it, I just realized that and was about to edit hah.
regarding the financialhighlights = highlights.findParent("table")AttributeError: 'list' object has no attribute 'findParent' should I be using something else rather than the findParent command? Last edited by zem52887; May 23rd, 2006 at 3:19 PM. |
|
|
|
|
|
#139 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Let me jump in here. highlights, as I understand it, is not a BeautifulSoup tree, but a Python list. As such, you can perform list operations on it, but not tree operations.
Clicky. |
|
|
|
|
|
#140 |
|
Hobbyist Programmer
Join Date: May 2006
Posts: 127
Rep Power: 3
![]() |
wahoooooooooo I think I got it, I gotta test it with a page that doesn't have financial highlights but:
#Financial Highlights
highlights = soup.firstText(re.compile("Highlights"))
fhighlights = highlights.findParent("table")
z = len(highlights)
if z == 0:
print "N/A"
else:
print fhighlights... victory! tested and works! Now I have to get this thing into a csv... |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|