Python 使用BS4获取列表中的项目导致AttributeError
我正试图从维基百科的一篇文章中为今天的日期搜集信息。当我使用BS4从页面获取信息时,我正在使用一种方法来查找第二个ul(这对应于“事件”部分中的所有文本)。我需要文章这一部分的文本。我当前的代码如下:Python 使用BS4获取列表中的项目导致AttributeError,python,beautifulsoup,Python,Beautifulsoup,我正试图从维基百科的一篇文章中为今天的日期搜集信息。当我使用BS4从页面获取信息时,我正在使用一种方法来查找第二个ul(这对应于“事件”部分中的所有文本)。我需要文章这一部分的文本。我当前的代码如下: time = datetime.now() day = time.strftime('%B') + '_' + str(int(time.strftime('%d'))) Label(text = 'ON THIS DAY', font = ('Verdana 12 bold')).grid(co
time = datetime.now()
day = time.strftime('%B') + '_' + str(int(time.strftime('%d')))
Label(text = 'ON THIS DAY', font = ('Verdana 12 bold')).grid(column = 1, row = 1, in_ = frame2, padx = 10)
url = 'https://en.wikipedia.org/wiki/' + str(day)
res = requests.get(url)
something = bs4.BeautifulSoup(res.text, features="html.parser")
events = something.find_all('ul')[1]
x = [x.text for x in events]
print(x)
上面显示的代码在python中出现以下错误:
Traceback (most recent call last):
File "D:\Program Files\Python\Python37\MyScripts\RSSFeed\RSSFeed.py", line 74, in <module>
load()
File "D:\Program Files\Python\Python37\MyScripts\RSSFeed\RSSFeed.py", line 71, in load
onthisday()
File "D:\Program Files\Python\Python37\MyScripts\RSSFeed\RSSFeed.py", line 64, in onthisday
x = [x.text for x in events]
File "D:\Program Files\Python\Python37\MyScripts\RSSFeed\RSSFeed.py", line 64, in <listcomp>
x = [x.text for x in events]
File "D:\Program Files\Python\Python37\lib\site-packages\bs4\element.py", line 742, in __getattr__
self.__class__.__name__, attr))
AttributeError: 'NavigableString' object has no attribute 'text'
回溯(最近一次呼叫最后一次):
文件“D:\Program Files\Python\Python37\MyScripts\RSSFeed\RSSFeed.py”,第74行,在
加载()
文件“D:\Program Files\Python\Python37\MyScripts\RSSFeed\RSSFeed.py”,第71行,处于加载状态
星期六
文件“D:\Program Files\Python\Python37\MyScripts\RSSFeed\RSSFeed.py”,第64行,在onthisday中
x=[x.text代表事件中的x]
文件“D:\Program Files\Python\Python37\MyScripts\RSSFeed\RSSFeed.py”,第64行,在
x=[x.text代表事件中的x]
文件“D:\Program Files\Python\Python37\lib\site packages\bs4\element.py”,第742行,位于__
self.\uuuuu类\uuuuuu.\uuuuuu名称\uuuuuuuuuu属性)
AttributeError:“NavigableString”对象没有属性“text”
我知道这个错误是因为事件只是列表中的一个项目,但是我该如何解决这个问题呢?
(顺便说一句,我看了其他问题的答案,我的答案都是一样的。)当你做
汤的时候,找到所有的('ul')[1]
,你就抓住了这个特定的元素。一旦你这样做了,就没有什么可重复的了,除非你再做一次find\u all
。您可以将整个内容转换为文本,然后在每一行上拆分
import requests
import bs4
response = requests.get('https://en.wikipedia.org/wiki/January_14')
soup = bs4.BeautifulSoup(response.text, 'html.parser')
events = soup.find_all('ul')[1]
events_list = events.text.split('\n')
print(events_list)
或者,如果您确实想像最初计划的那样理解列表,您必须在事件中找到所有这些标记(我选择了
),然后您可以迭代这些标记:
import requests
import bs4
response = requests.get('https://en.wikipedia.org/wiki/January_14')
soup = bs4.BeautifulSoup(response.text, 'html.parser')
events = soup.find_all('ul')[1]
indv_event = events.find_all('li')
x = [x.text for x in indv_event]
因此,您的完整代码(显然看起来更复杂,但仅此部分就可以让您继续):
from datetime import datetime
time = datetime.now()
day = time.strftime('%B') + '_' + str(int(time.strftime('%d')))
# Not too familiar with this line. Looks like for tKinter
Label(text = 'ON THIS DAY', font = ('Verdana 12 bold')).grid(column = 1, row = 1, in_ = frame2, padx = 10)
url = 'https://en.wikipedia.org/wiki/' + str(day)
res = requests.get(url)
something = bs4.BeautifulSoup(res.text, features="html.parser")
events = something.find_all('ul')[1]
indv_event = events.find_all('li')
x = [x.text for x in indv_event]
print(x)