解析时如何选择一个(BS4,Python)
我正在尝试解析数据,但出现了很奇怪的错误。我发现变量“a”具有类型 bs4.element.Tag
并包含一些数据。但是,当我尝试应用任何函数时(我尝试使用 strip()
),我收到错误
TypeError: 'NoneType' object is not callable
为什么我会收到它如果我看到 a 包含数据?
我的代码:
import pandas as pd
import requests
import bs4
session = requests.Session()
session.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36',
'Accept-Language': 'ru'}
url = 'https://tvoe.ru//catalog/jenshchinam/odejda/yubka/973977/?oid=973978'
res = session.get(url=url)
res.raise_for_status()
text = res.text
soup = bs4.BeautifulSoup(text, 'lxml')
container = soup.select(
'div.container_inner.clearfix ')
a = (container[0].select_one('a.pc-category-block__title'))
如果尝试查看 Юбки
。
我想通过使用 a.strip('>')
获取 Юбки
。但总是得到 TypeError: 'NoneType' object is not callable
I am trying to parse data, but get so strange error. I se that varuable "a" has type bs4.element.Tag
and contains some data. However when I try to aplly any function (I am trying to use strip()
) I get an error
TypeError: 'NoneType' object is not callable
Why do I get it if I see that a contains data?
My code:
import pandas as pd
import requests
import bs4
session = requests.Session()
session.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36',
'Accept-Language': 'ru'}
url = 'https://tvoe.ru//catalog/jenshchinam/odejda/yubka/973977/?oid=973978'
res = session.get(url=url)
res.raise_for_status()
text = res.text
soup = bs4.BeautifulSoup(text, 'lxml')
container = soup.select(
'div.container_inner.clearfix ')
a = (container[0].select_one('a.pc-category-block__title'))
What I see if try to look at a <a class="pc-category-block__title" href="/catalog/jenshchinam/odejda/yubka/">Юбки</a>
.
I want to get Юбки
by using a.strip('>')
. But always get TypeError: 'NoneType' object is not callable
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
.text
属性,如果您需要从标签中访问用户,那就是输出
You might use
.text
attribute if you need to access text visible to user from tag, that isoutput