[英]AttributeError: 'ResultSet' object has no attribute 'find_all' Beautifulsoup
[英]BeautifulSoup Scraper can't find text?AttributeError: ResultSet object has no attribute 'find_all'
对编程非常陌生,对任何不良做法感到抱歉:
我试图制作一个 web 刮刀,它确实会刮。com 用于我所在领域的工作列表,并在网上关注了一些关于它的文章,我以为我理解了,但现在我想我有一个误解。
我正在尝试抓取在 html 中找到的作业的位置,如下所示: html 代码
为了抓取该位置,我被告知要执行以下操作:
grabbing location name
c = div.find_all(name="span",attrs={"class":"location"})
for span in c:
print(span.text)
job_post.append(span.text)
但是我注意到有时网页会在 div 下加载它,而不是 span,所以我将代码编辑如下:
def find_location_for_job(self,div,job_post,city):
div2 = div.find_all(name="div",attrs={"class":"sjcl"})
print(div2)
try:
div3 = div2.find_all(name="div",attrs={"class":"location accessible-contrast-color-location"})
job_post.append(div3.text)
except:
span = div2.find_all(name="span",attrs={"class":"location accessible-contrast-color-location"})
job_post.append(span.text)
print(job_post)
然而,有一半的时间它仍然说它无法在 div/span 中找到文本,即使我搜索帖子并看到它被标记为一个或另一个。
AttributeError: ResultSet object has no attribute 'find_all'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?
请注意,我留下了找到的代码,因为当使用 div 而不是 span 时它不会捕获结果。 所以我的下一个故障排除步骤是将我的想法与他们的想法结合起来,如下所示:
def find_location_for_job(self,div,job_post,city):
div2 = div.find_all(name="div",attrs={"class":"sjcl"})
try:
div3 = div2.find_all(name="div",attrs={"class":"location accessible-contrast-color-location"})
for span in div3:
job_post.append(span.text)
except:
div4 = div.findAll("span",attrs={"class":"location accessible-contrast-color-location"})
for span in div4:
job_post.append(span.text)
但是,此方法将整个位置列表扔到它抓取的每个条目中(它每个城市抓取 10 个帖子,因此此方法将 10 个位置扔到 10 个帖子条目中的每个条目中)
谁能告诉我我在哪里放屁?
编辑:pastebin 中的完整代码: https://pastebin.com/0LLb9ZcU
div2
是一个ResultSet
,因为当您使用 BeautifulSoup 的find_all
方法时,它会返回。 您需要遍历ResultSet
并搜索内部字段,如下所示:
def find_location_for_job(self, div, job_post, city):
div2 = div.find_all(name="div",attrs={"class":"sjcl"})
for sjcl_div in div2:
div3 = div2.find_all(name="div",attrs={"class":"location accessible-contrast-color-location"})
div4 = div.find_all("span",attrs={"class":"location accessible-contrast-color-location"})
if div3:
for span in div3:
job_post.append(span.text)
elif div4:
for span in div4:
job_post.append(span.text)
else:
print("Uh-oh, couldn't find the tags!")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.