【问题标题】:Unsupported or invalid CSS selector: "_a" while scraping angel list不支持或无效的 CSS 选择器:抓取 angellist 时的“a”
【发布时间】:2019-01-08 06:29:03
【问题描述】:

我一直在从天使 csv 文件中抓取公司数据,我想获取创始人的名字、姓氏和职位头衔。为此,我将 Requests 与 Beautiful Soup 一起使用。我认为我对soup.select 做错了什么。

这是当前的嵌套类树

-founders section
--section with_filler with_editable_regions dsss17 startups-show-sections ffs70 founders _a _jm
---dsr31 startup_roles fsp87 startup_profile_group _a _jm
----ul.larger roles
-----li.role
------<<dynamic div>>
-------g-lockup top larger
--------photo
--------text
---------name
---------role_title
---------bio

这里是示例页面 URL https://angel.co/dealflicks

import requests
from bs4 import BeautifulSoup, element
req = requests.get('https://angel.co/dealflicks', headers={'User-Agent': 'Mozilla/5.0'})
print(req.status_code)
soup = BeautifulSoup(req.text,"lxml")

founders = soup.select('.founders section .section with_filler with_editable_regions dsss17 startups-show-sections ffs70 founders _a _jm .dsr31 startup_roles fsp87 startup_profile_group _a _jm .larger roles role')

print (founders)

这是给我这个错误

Traceback (most recent call last):
  File "hello.py", line 11, in <module>
    founders = soup.select('.founders section .section with_filler 
with_editable_regions dsss17 startups-show-sections ffs70 founders _a _jm 
.dsr31 startup_roles fsp87 startup_profile_group _a _jm .larger roles 
role')
  File "C:\Users\nandi\Anaconda3\lib\site-packages\bs4\element.py", line 
1477, in select
    'Unsupported or invalid CSS selector: "%s"' % token)
ValueError: Unsupported or invalid CSS selector: "_a"

【问题讨论】:

  • 您应该了解 CSS 选择器的工作原理。在您的情况下,_a 将匹配不允许的 &lt;_a&gt; 标记。

标签: python web-scraping beautifulsoup python-requests


【解决方案1】:

这是因为_a 是类而不是标签名称或&lt;_a&gt;,您需要在类的值前面加上点. 或与findAll() 一起使用

soup.findAll('div', class='section with_filler with_editable_regions dsss17 startups-show-sections ffs70 founders _a _jm')

但你只需要这个简单的选择器

founders = soup.select('ul.larger.roles li')
print (founders)

或从 Web 开发者工具的元素面板中复制选择器

founders = soup.select('#root > div.page.flush_bottom.dl85.layouts.fhr17.header._a._jm > div > div.content.s-grid.s-grid--outer.u-maxWidthLayout.s-vgBottom2 > div > div.s-flexgrid0.s-flexgrid0--fixed.panes_grid > div.main.pane.s-flexgrid0--footer.s-flexgrid0-colMdW.s-vgPadRight1 > div > div.founders.section > div > div > ul > li')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 2022-08-17
    • 2015-05-02
    • 2019-02-08
    • 1970-01-01
    • 2011-11-22
    相关资源
    最近更新 更多