Skip to main content
 首页 » 编程设计

python之刮痧确实有美汤

2025年05月04日25lhb25

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

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"})) 

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

请您参考如下方法:

首先,我用一个作业搜索所有元素的 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 '----------'