【问题标题】:Problem with data extraction from Indeed by BeautifulSoupBeautifulSoup 从 Indeed 中提取数据的问题
【发布时间】:2019-01-13 21:16:16
【问题描述】:

我正在尝试从 Indeed 网站上提取每个帖子的职位描述,但结果不是我所期望的!

我编写了一个代码来获取职位描述。我正在使用 python 2.7 和最新的 beautifulsoup。当您打开页面并单击每个职位时,您将在屏幕右侧看到相关信息。我需要为该页面上的每个工作提取这些工作描述。我的代码:

import sys

import urllib2 

from BeautifulSoup import BeautifulSoup

url = "https://www.indeed.com/jobs?q=construction%20manager&l=Houston%2C%20TX&vjk=8000b2656aae5c08"

html = urllib2.urlopen(url).read()

soup = BeautifulSoup(html)

N = soup.findAll("div", {"id" : "vjs-desc"})

print N

我希望看到结果,但结果却是 []。是不是因为 Id 不唯一。如果是这样,我应该如何编辑代码?

【问题讨论】:

    标签: python beautifulsoup urllib2


    【解决方案1】:

    #vjs-desc 元素由 javascript 生成,内容来自 Ajax 请求。要获得描述,您需要执行该请求。

    # -*- coding: utf-8 -*-
    
    # it easier to create http request/session using this
    import requests
    import re, urllib
    from BeautifulSoup import BeautifulSoup
    
    url = "https://www......"
    
    # create session
    s = requests.session()
    html = s.get(url).text
    
    # exctract job IDs
    job_ids = ','.join(re.findall(r"jobKeysWithInfo\['(.+?)'\]", html))
    ajax_url = 'https://www.indeed.com/rpc/jobdescs?jks=' + urllib.quote(job_ids)
    # do Ajax request and convert the response to json 
    ajax_content = s.get(ajax_url).json()
    print(ajax_content)
    
    for id, desc in ajax_content.items():
        print id
        soup = BeautifulSoup(desc, 'html.parser')
        # or try this
        # soup = BeautifulSoup(desc.decode('unicode-escape'), 'html.parser')
        print soup.text.encode('utf-8')
        print('==============================')
    

    【讨论】:

    • 感谢您的回复。我收到此错误消息:soup = BeautifulSoup(desc.decode('unicode-escape'), 'html.parser') UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 609: ordinal not in范围(128)@ewwink
    • 我在开头添加了 import urllib 并删除了您要求的内容,但仍然出现另一个错误:文件“c:\python27\lib\site-packages\BeautifulSoup.py”,第 1344 行,在unknown_starttag and (self.parseOnlyThese.text or not self.parseOnlyThese.searchTag(name, attrs)): AttributeError: 'str' object has no attribute 'text' @ewwink
    • 但它打印部分结果对吗?尝试在第一行添加# -*- coding: utf-8 -*-,然后再次添加.decode('unicode-escape')
    • 回答已编辑,看看,考虑使用python 3,因为python 2.7有时会出现字符编码问题。
    • 第 2 页附加 &start=10 到搜索 URL,第 3 页附加 &start=20 等等
    猜你喜欢
    • 2020-11-23
    • 2015-01-21
    • 2020-05-08
    • 2021-04-28
    • 2020-12-21
    • 2021-09-28
    • 2019-06-02
    • 2019-09-26
    • 2012-06-10
    相关资源
    最近更新 更多