【发布时间】: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将匹配不允许的<_a>标记。
标签: python web-scraping beautifulsoup python-requests