【发布时间】:2020-09-26 21:56:10
【问题描述】:
对编程非常陌生,对任何不良做法感到抱歉:
我正在尝试制作一个网络爬虫,它可以在 Indeed.com 上搜索我所在领域的职位列表,并在网上关注了一些关于它的文章,我以为我理解了,但现在我想我有一个误解。
我正在尝试抓取我在 html 中找到的作业的位置,如下所示: html code
为了抓取该位置,我被告知要执行以下操作:
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
【问题讨论】:
-
优秀的第一Q!作为初学者程序员,您做得很好。保持良好的工作并继续发布组织良好的 Q。你是班级的功劳!
标签: python html web-scraping beautifulsoup screen-scraping