我试图让这个工作,但没有用,有人可以帮忙吗?
number = 0
statement = "i will go there",number,"times"
for number in statement:
if number < 5:
print (statement)
number += 1
我在这里要做的是首先建立一个通用声明(我将在那里进行多次)。我想更改号码&#39;在这个语句中,0,1,2,3,4,5最终打印出来:
i will go there 0 times
i will go there 1 times
i will go there 2 times
i will go there 3 times
i will go there 4 times
i will go there 5 times
但我收到错误代码:
TypeError: '<' not supported between instances of 'str' and 'int'
感谢所有的反馈,伙计们。你看,我正在使用这个例子来理解我正在研究的另一组代码。代码如下:
import time
from selenium import webdriver
chrome_path = r"C:\Users\lnv\AppData\Local\Programs\Selenium Chrome
Driver\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get('http://www.google.com');
search_box = driver.find_element_by_name('q')
search_box.send_keys(input('What do you want to search?'))
search_box.submit()
time.sleep(5)
## Prints info seen on Page 1 of search result
##result = driver.find_elements_by_class_name('st')
##for post in result:
## print (post.text)
## Prints the info seen on Page 2 of search result, video suggested using a FOR loop to automatically scrape and print info from other page
##driver.find_element_by_xpath('//*[@id="nav"]/tbody/tr/td[3]/a').click()
##result2 = driver.find_elements_by_class_name('st')
##for post in result2:
## print (post.text)
x = 1
new_page ="""driver.find_elements_by_xpath('//[@id="nav"]/tbody/tr/td[{}]/a').click()"""
while (x<6):
exec(new_page.format(x))
result3 = driver.find_elements_by_class_name('st')
for post in result3:
print (post.text)
x +=1
我从观看这个Youtube教程中得到了这个想法:https://www.youtube.com/watch?v=O--WVte1WhU
我们的想法是抓取谷歌搜索结果网页上显示的信息(班级名称:&#39; st&#39;),但是我想抓一下,直到谷歌搜索结果的第5页。该视频建议使用For循环自动从其他页面抓取和打印信息。我设法根据&#39; molbdnilo&#39;的答案建议使用.format(x)更改页码。但是,新页面(存储在new_page中)存储为列表,因此无法执行。我得到的错误信息是:
AttributeError: 'list' object has no attribute 'click'
我尝试使用以下代码进行一些故障排除:
x = 1
new_page = """print('dog{}')"""
while (x<6):
exec(new_page.format(x))
x +=1
印刷表现非常好:
DOG1
DOG2
dog3
dog4
dog5
我不明白为什么它无法在我的抓取代码中工作。 new_page应该在更改页码并执行时循环超过1-6。
有人可以帮忙吗?
答案 0 :(得分:1)
number in statement
并不意味着“我创建'声明'时使用的变量'数字'”。
for number in statement
遍历statements
的元素,每次迭代number
都会引用该元素。
打开翻译,看看你在做什么。
>>> number = 0
>>> statement = "i will go there",number,"times"
>>> statement
('i will go there', 0, 'times')
所以statement
是一个带有树元素的元组
第二个元素保存number
创建时的值。
>>> number = 1000
>>> statement
('i will go there', 0, 'times')
请注意statement
的第二个元素没有改变。
>>> for number in statement: print 'number is', number
...
number is i will go there
number is 0
number is times
这会循环遍历statement
的元素。
要完成您想要的任务,请在循环内创建一个字符串
如果您想要在以后添加数字的一般声明,请使用format
。
这样的事情:
statement = "i will go there {} times"
for number in range(1,6):
print statement.format(number)
答案 1 :(得分:0)
如果你想打印像。
I will go there 0 times
I will go there 1 times
I will go there 2 times
In[126]: for n in range(5):
...: print('I will go there ' + str(n) + ' times')
...:
I will go there 0 times
I will go there 1 times
I will go there 2 times
I will go there 3 times
I will go there 4 times
告诉我这是否是你期待的答案
答案 2 :(得分:0)
for number in range(5):
print("i will go there {} times".format(number))
答案 3 :(得分:0)
试试这个:
statement = "i will go there {} times"
for number in range(6):
print (statement.format(number))
答案 4 :(得分:0)
您可以尝试这种方式:
number = 5
statement = "i will go there "
for i in range(10):
if i <= number:
print(statement + str(i) + " time")
答案 5 :(得分:0)
终于能够让整个代码工作了。工作代码如下:
import time
from selenium import webdriver
chrome_path = r"C:\Users\lnv\AppData\Local\Programs\Selenium Chrome
Driver\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get('http://www.google.com');
search_box = driver.find_element_by_name('q')
search_box.send_keys(input('What do you want to search?'))
search_box.submit()
time.sleep(5)
result = driver.find_elements_by_class_name('st')
for post in result:
print (post.text)
for x in range (3,6):
new_page = repr('//*[@id="nav"]/tbody/tr/td[{}]/a').format(x)
data = [exec('driver.find_element_by_xpath('+new_page+').click()')]
for i in data:
result3 = driver.find_elements_by_class_name('st')
for post in result3:
print (post.text)
driver.quit()
当Youtube视频作者说For循环自动化页面时,我应该只自动化Xpath部分。傻傻的......我花了很长时间才意识到这一点。
PS:新手在这里,我只用了1个月的Python。