我想让这个工作,但无济于事,有人可以帮忙吗?有人能告诉我这段代码有什么问题吗?
number = 0
statement = "i will go there",number,"times"
for number in statement:
if number < 5:
print (statement)
number += 1
我在这里要做的是先建立一个通用语句(我会去那里数次)。我想这个说法里面的“号”变更为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
的想法是刮看到一个谷歌搜索结果网页上的信息(类名称:“ST”) ,但我想刮到谷歌搜索结果的第5页。该视频建议使用For循环来自动从其他页面中剪切和打印信息。我设法通过使用.format(x)来改变页码,正如'molbdnilo'的答案所示。但是,新页面(存储在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)和(对于语句中的数字),您都使用相同的变量。所以当数字<5被执行时,数字保存来自语句的子字符串! –
还需要缩进打印语句。 – hallaksec