【问题标题】:Scraping Indeed with Beautiful Soup用美丽的汤刮确实
【发布时间】:2016-11-10 00:02:35
【问题描述】:

我不熟悉 html 和 web 抓取漂亮的汤。我正在尝试从各种确实的职位发布中检索职位、薪水、地点和公司名称。到目前为止,这是我的代码:

URL = "http://www.indeed.com/jobs?q=data+scientist+%2420%2C000&l=New+York&start=10"
import urllib2
import bs4
from bs4 import BeautifulSoup
soup = BeautifulSoup(urllib2.urlopen(URL).read())
resultcol = soup.find_all(id = 'resultsCol')
company = soup.findAll('span', attrs={"class":"company"})
jobs = (soup.find_all({'class': " row result"}))

虽然我有寻找工作和公司的命令,但我无法获得内容。我知道有一个内容命令,但到目前为止我的变量都没有这个属性。谢谢!

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:

    首先我用一个作业搜索所有元素 div,然后在 div 中搜索元素

    import urllib2
    from bs4 import BeautifulSoup
    
    URL = "http://www.indeed.com/jobs?q=data+scientist+%2420%2C000&l=New+York&start=10"
    
    soup = BeautifulSoup(urllib2.urlopen(URL).read(), 'html.parser')
    
    results = soup.find_all('div', attrs={'data-tn-component': 'organicJob'})
    
    for x in results:
        company = x.find('span', attrs={"itemprop":"name"})
        print 'company:', company.text.strip()
    
        job = x.find('a', attrs={'data-tn-element': "jobTitle"})
        print 'job:', job.text.strip()
    
        salary = x.find('nobr')
        if salary:
            print 'salary:', salary.text.strip()
    
        print '----------'
    

    【讨论】:

    • 谢谢!知道如何刮工资吗?以下是它的嵌套方式:' $88,305 - $146,570 一年 '
    • 我也试过了——它有效。但是大多数工作没有工资,所以你必须使用if来查看它。
    【解决方案2】:

    更新了@furas 示例,用于 python3:

    import urllib.request
    from bs4 import BeautifulSoup
    
    URL = "https://www.indeed.com/jobs?q=data+scientist+%2420%2C000&l=New+York&start=10"    
    
    soup = BeautifulSoup(urllib.request.urlopen(URL).read(), 'html.parser')
    
    results = soup.find_all('div', attrs={'data-tn-component': 'organicJob'})
    
    for x in results:
    
        company = x.find('span', attrs={"class":"company"})
        if company:
            print('company:', company.text.strip() )
    
        job = x.find('a', attrs={'data-tn-element': "jobTitle"})
        if job:
            print('job:', job.text.strip())
    
        salary = x.find('nobr')
        if salary:
            print('salary:', salary.text.strip())
    
        print ('----------')
    

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签