请求多页地址,在不更改 url 的情况下更改页面
Request to a multi-page address that changes pages without changing the url
我想申请这个 url:
https://www.codal.ir/CompanyList.aspx
此 url 包含 110 页的表格,当页面更改时,url 和新请求都不会更改。
这是我的代码:
import requests as req
req = req.Session()
isics = req.get("https://www.codal.ir/CompanyList.aspx")
print(isics.text)
但我只得到第一页 information.I 打算通过请求和正则表达式从表格中提取所需的信息,但如果你有其他方法,我会很高兴听到。谢谢你帮助我得到整个页数。
我使用 Selenium
在 table 中导航。您不能使用 requests
执行此操作,因为我们没有将我们重定向到 table 中的新页面的链接。您可以在下面找到代码。
from bs4 import BeautifulSoup
from selenium import webdriver
import time
def get_company_links(links, driver):
soup = BeautifulSoup(driver.page_source, "html.parser")
rows = soup.select("table.companies-table tr")
for row in rows:
link = row.select_one("a")
if(link):
links.append("https://www.codal.ir/" + link['href'])
options = webdriver.ChromeOptions()
#options.add_argument("--headless")
driver = webdriver.Chrome(options=options)
driver.get("https://www.codal.ir/CompanyList.aspx")
current_page_button = driver.find_element_by_css_selector('input[type="submit"].normal.selected')
page_number = int(current_page_button.get_attribute('value'))
while(True):
get_company_links(links, driver)
next_page_button = driver.find_element_by_css_selector('input#ctl00_ContentPlaceHolder1_ucPager1_btnNext')
next_page_button.click()
time.sleep(2)
previous_page_number = page_number
current_page_button = driver.find_element_by_css_selector('input[type="submit"].normal.selected')
page_number = int(current_page_button.get_attribute('value'))
if(previous_page_number == page_number):
break # no more page left
print(links)
主要工作原理是浏览table和收集公司网站的链接。我们使用next
按钮导航,当最后一页索引等于当前索引时停止,这表明我们到达了table的末尾。
我想申请这个 url:
https://www.codal.ir/CompanyList.aspx
此 url 包含 110 页的表格,当页面更改时,url 和新请求都不会更改。
这是我的代码:
import requests as req
req = req.Session()
isics = req.get("https://www.codal.ir/CompanyList.aspx")
print(isics.text)
但我只得到第一页 information.I 打算通过请求和正则表达式从表格中提取所需的信息,但如果你有其他方法,我会很高兴听到。谢谢你帮助我得到整个页数。
我使用 Selenium
在 table 中导航。您不能使用 requests
执行此操作,因为我们没有将我们重定向到 table 中的新页面的链接。您可以在下面找到代码。
from bs4 import BeautifulSoup
from selenium import webdriver
import time
def get_company_links(links, driver):
soup = BeautifulSoup(driver.page_source, "html.parser")
rows = soup.select("table.companies-table tr")
for row in rows:
link = row.select_one("a")
if(link):
links.append("https://www.codal.ir/" + link['href'])
options = webdriver.ChromeOptions()
#options.add_argument("--headless")
driver = webdriver.Chrome(options=options)
driver.get("https://www.codal.ir/CompanyList.aspx")
current_page_button = driver.find_element_by_css_selector('input[type="submit"].normal.selected')
page_number = int(current_page_button.get_attribute('value'))
while(True):
get_company_links(links, driver)
next_page_button = driver.find_element_by_css_selector('input#ctl00_ContentPlaceHolder1_ucPager1_btnNext')
next_page_button.click()
time.sleep(2)
previous_page_number = page_number
current_page_button = driver.find_element_by_css_selector('input[type="submit"].normal.selected')
page_number = int(current_page_button.get_attribute('value'))
if(previous_page_number == page_number):
break # no more page left
print(links)
主要工作原理是浏览table和收集公司网站的链接。我们使用next
按钮导航,当最后一页索引等于当前索引时停止,这表明我们到达了table的末尾。