python - 使用 beautifulsoup 解析 HTML 给出 "None"

标签 python web-scraping beautifulsoup

我可以清楚地看到我需要的标签,以便获取我想要抓取的数据。

根据多个教程,我的做法完全相同。

那么,当我只想在 li 类之间显示代码时,为什么它给我“无”

from bs4 import BeautifulSoup
import requests

    response = requests.get("https://www.governmentjobs.com/careers/sdcounty")
    soup = BeautifulSoup(response.text,'html.parser')

    job = soup.find('li', attrs = {'class':'list-item'})
    print(job)

enter image description here

最佳答案

虽然页面会动态更新(它会从浏览器发出额外的请求来更新您没有通过单个请求捕获的内容),但您可以在网络选项卡中找到感兴趣内容的源 URI。您还需要添加预期的 header 。

import requests
from bs4 import BeautifulSoup as bs

headers = {'X-Requested-With': 'XMLHttpRequest'}
r = requests.get('https://www.governmentjobs.com/careers/home/index?agency=sdcounty&sort=PositionTitle&isDescendingSort=false&_=', headers=headers)
soup = bs(r.content, 'lxml')
print(len(soup.select('.list-item')))

关于python - 使用 beautifulsoup 解析 HTML 给出 "None",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58783950/

相关文章:

python - 尽管遵循安装说明,Beaker 仍无法找到 Python 和 Julia 安装

RSelenium 找不到具有给定参数的元素

clojure - 使用 enlive 时删除标记

python - Scrapy:捕获具有特定 HTTP 服务器代码的响应

python - 使用位掩码计数重置?

python - 如何使用Python中另一列的值填充pandas数据框中的空值?

python - urllib2 中的未知 url 类型错误

python - 如何抓取 - 跨 aria-hidden ="true"- 文本

python - 如何将来自 python 的值插入 html 文件的主体部分?

python - 如何使用 PyQt 的 Phonon 播放一首简单的歌曲?