Данные, которые вы хотите, чтобы очистить динамически загружается с помощью Ajax, что вам нужно сделать много работы, чтобы добраться до HTML вы на самом деле хотите:
import requests
from bs4 import BeautifulSoup
header = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
}
with requests.Session() as s:
r = s.get("https://angel.co/companies").content
csrf = BeautifulSoup(r).select_one("meta[name=csrf-token]")["content"]
header["X-CSRF-Token"] = csrf
ids = s.post("https://angel.co/company_filters/search_data", data={"sort": "signal"}, headers=header).json()
_ids = "".join(["ids%5B%5D={}&".format(i) for i in ids.pop("ids")])
rest = "&".join(["{}={}".format(k,v) for k,v in ids.items()])
url = "https://angel.co/companies/startups?{}{}".format(_ids, rest)
rsp = s.get(url, headers=header)
print(rsp.json())
Мы должны сначала получить действительный CSRF-маркер которая является то, что делает первоначальный запрос, то нам нужно разместить на https://angel.co/company_filters/search_data
:
, который дает нам:
{"ids":[296769,297064,60,63,112,119,130,160,167,179,194,236,281,287,312,390,433,469,496,516],"total":908164,"page":1,"sort":"signal","new":false,"hexdigest":"3f4980479bd6dca37e485c80d415e848a57c43ae"}
Они PARAMS необходимы для нашего добраться до https://angel.co/companies/startups
то есть наш последний запрос:
Этот запрос затем дает нам больше JSON, который держит HTML и всю информацию о компании:
{"html":"<div class=\" dc59 frs86 _a _jm\" data-_tn=\"companies/results ...........
Существует слишком много сообщений, но это то, что вам нужно проанализировать.
Так положить все это вместе:
In [3]: header = {
...: "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36",
...: "X-Requested-With": "XMLHttpRequest",
...: }
In [4]: with requests.Session() as s:
...: r = s.get("https://angel.co/companies").content
...: csrf = BeautifulSoup(r, "lxml").select_one("meta[name=csrf-token]")["content"]
...: header["X-CSRF-Token"] = csrf
...: ids = s.post("https://angel.co/company_filters/search_data", data={"sort": "signal"}, headers=header).json()
...: _ids = "".join(["ids%5B%5D={}&".format(i) for i in ids.pop("ids")])
...: rest = "&".join(["{}={}".format(k, v) for k, v in ids.items()])
...: url = "https://angel.co/companies/startups?{}{}".format(_ids, rest)
...: rsp = s.get(url, headers=header)
...: soup = BeautifulSoup(rsp.json()["html"], "lxml")
...: for comp in soup.select("div.base.startup"):
...: text = comp.select_one("div.text")
...: print(text.select_one("div.name").text.strip())
...: print(text.select_one("div.pitch").text.strip())
...:
Frontback
Me, now.
Outbound
Optimizely for messages
Adaptly
The Easiest Way to Advertise Across The Social Web.
Draft
Words with Friends for Fantasy (w/ real money)
Graphicly
an automated ebook publishing and distribution platform
Appstores
App Distribution Platform
eVenues
Online Marketplace & Booking Engine for Unique Meeting Spaces
WePow
Video & Mobile Recruitment
DoubleDutch
Event Marketing Automation Software
ecomom
It's all good
BackType
Acquired by Twitter
Stipple
Native advertising for the visual web
Pinterest
A Universal Social Catalog
Socialize
Identify and reward your most influential users with our drop-in social platform.
StyleSeat
Largest and fastest growing marketplace in the $400B beauty and wellness industry
LawPivot
99 Designs for legal
Ostrovok
Leading hotel booking platform for Russian-speakers
Thumb
Leading mobile social network that helps people get instant opinions
AppFog
Making developing applications on the cloud easier than ever before
Artsy
Making all the world’s art accessible to anyone with an Internet connection.
Насколько пагинация идет, вы ограничены до 20 страниц в день, но, чтобы получить все 20 страниц просто случай добавления page:page_no
наших данных формы получить новый PARAMS необходим, data={"sort": "signal","page":page}
, при нажатии кнопки нагрузки больше вы можете увидеть, что размещена:
Таким образом, окончательный код:
import requests
from bs4 import BeautifulSoup
def parse(soup):
for comp in soup.select("div.base.startup"):
text = comp.select_one("div.text")
yield (text.select_one("div.name").text.strip()), text.select_one("div.pitch").text.strip()
def connect(page):
header = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
}
with requests.Session() as s:
r = s.get("https://angel.co/companies").content
csrf = BeautifulSoup(r, "lxml").select_one("meta[name=csrf-token]")["content"]
header["X-CSRF-Token"] = csrf
ids = s.post("https://angel.co/company_filters/search_data", data={"sort": "signal","page":page}, headers=header).json()
_ids = "".join(["ids%5B%5D={}&".format(i) for i in ids.pop("ids")])
rest = "&".join(["{}={}".format(k, v) for k, v in ids.items()])
url = "https://angel.co/companies/startups?{}{}".format(_ids, rest)
rsp = s.get(url, headers=header)
soup = BeautifulSoup(rsp.json()["html"], "lxml")
for n, p in parse(soup):
yield n, p
for i in range(1, 21):
for name, pitch in connect(i):
print(name, pitch)
Очевидно, что вы разбираетесь с вами, но все, что вы видите в своем браузере в результатах, будет доступно.
Над этим сценарием, если полезно, это: фильтры: функция (S, L) { вар с, и, д, ч, р, е, ж, м, V, Y, б, _, ш , x, C, A, k, S, N, T, E, D, I, $, P, M; return u = new o (s ("показывается в настоящее время"), l.data ("sort")), u.set_data (l.data ("init_data")), u.render ({ fetch :! l.data ("new") }), – taylorhamcheese