【发布时间】:2018-04-06 00:22:10
【问题描述】:
我正在尝试打印作业的文本。不幸的是,href 一直没有显示,并且我在打印文本时不断收到错误,我收到以下错误:
Traceback (most recent call last):
File "C:/Users/Bain3/PycharmProjects/untitled4/Bookmaker improved ALTERNATIVE.py",
line 155, in <module>
print(bet.text)
AttributeError: 'str' object has no attribute 'text'
以下代码或完整代码here
with open('Aperture Science.csv', 'a+', newline='\n') as file:
writer = csv.writer(file)
for section in sections:
try:
link = section.find_element_by_css_selector("h3 a").get_attribute("href")
print((section.get_attribute('href')))
except NoSuchElementException:
pass
time.sleep(7)
try:
team_name = section.find_element_by_css_selector(".row:nth-child(1) td:nth-child(1)").text
print(section.text)
except NoSuchElementException:
pass
time.sleep(7)
try:
bet = section.find_element_by_css_selector(".odds .odds span").text
print(bet.text)
except NoSuchElementException:
pass
time.sleep(7)
writer.writerow((bet, team_name, link))
我尝试过以下变体:
for section in sections:
try:
link = section.find_element_by_css_selector("h3 a").get_attribute("href")
print((link.get_attribute('href')))
还有:
team_name = section.find_element_by_css_selector(".row:nth-child(1) td:nth-child(1)").text
print(team_name.text)
print(section.text) 有效,尽管这不是我想要打印的文本。有什么想法吗?
【问题讨论】:
-
请发回错误的整个跟踪
-
@0TTT0 textuploader.com/d4ej7
-
您分配了
bet = section.find...(....).text,然后期望bet.text存在?
标签: python python-3.x csv selenium web-scraping