Python Embedded For range Loops -


i following commands grab date address in range can't seem run more once. using python 3. can see below the url site appended read http://zinc.docking.org/substance/10 ; http://zinc.docking.org/substance/11 ... , on. here code:

import bs4 bs import urllib.request site = "http://zinc.docking.org/substance/" in range(10, 16):      site1 = str("%s%i" % (site, i))     sauce = urllib.request.urlopen(site1).read()     soup = bs.beautifulsoup(sauce, 'lxml')     table1 = soup.find("table", attrs={"class": "substance-properties"}) row in table1.findall('tr'):     row1 = row.findall('td') ate = row1[0].gettext() print(ate) 

this output:

$python3 date.py november 11th, 2005 

the script should give me 3 dates. code works know row[0] in fact contain value.i feel there sort of simple formatting error not sure begin troubleshooting. when format "correctly" code:

import bs4 bs import urllib.request import pandas pd import csv site = "http://zinc.docking.org/substance/" in range(10, 16):      site1 = str("%s%i" % (site, i))     sauce = urllib.request.urlopen(site1).read()     soup = bs.beautifulsoup(sauce, 'lxml')     table1 = soup.find("table", attrs={"class": "substance-properties"})     table2 = soup.find("table", attrs={"class": "protomers"})     row in table1.findall('tr'):         row1 = row.findall('td')         ate = row1[0].gettext()         print(ate) 

the error follows:

traceback (most recent call last): file "stack.py", line 11, in <module> ate = row1[1].gettext() indexerror: list index out of range 

the first code works know row[0] in fact contain value. ideas?

you might want fix indentation:

import bs4 bs import urllib.request site = "http://zinc.docking.org/substance/" in range(10, 16):      site1 = str("%s%i" % (site, i))     sauce = urllib.request.urlopen(site1).read()     soup = bs.beautifulsoup(sauce, 'lxml')     table1 = soup.find("table", attrs={"class": "substance-properties"})     row in table1.findall('tr'):         row1 = row.findall('td')         date = row1[0].gettext()         print(date) 

edit: should rename date variable, reserved name. also, convention python vars lower case.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -