【发布时间】:2023-02-25 07:28:47
【问题描述】:
我正在尝试抓取此网站:[https://www.icriq.com/fr/][1]
我需要按公司名称搜索,并获取[此示例公司页面][2]中显示的公司详细信息。
我写了以下代码:
import requests
from bs4 import BeautifulSoup
api_url ='https://www.icriq.com/pls/owa_rib/ribw_recherche.rech_rap'
headers= {
"Content-Type":"application/x-www-form-urlencoded",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/110.0"}
body_first_page="p_lang=fr&p_portail=&p_inclus_req=N&p_ecoresp=N&p_ind_lpdq=N&p_mot_cle=agrimetal&p_type_rech=NOM&p_tab_alim_atpro=-1"
res = requests.post(api_url,data=body_first_page,headers=headers)
soup = BeautifulSoup(res.text,'lxml')
生成的汤具有以下 href,与“AGRIMETAL INC.”相关联:
href="/pls/owa_rib/ribwaff1.afficher_profil?p_id_req=60354405&p_cle=8POJTR9O2P"
但是,当我尝试使用它使用以下函数发出另一个请求时:
def get_soup(url):
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
#time.sleep(10)
html_page = urlopen(req).read()
#time.sleep(10)
soup = BeautifulSoup(html_page, 'html.parser')
return soup
我收到 HTTPError: HTTP Error 400: Bad Request 错误
[1]:https://www.icriq.com/fr/
[2]:https://www.icriq.com/pls/owa_rib/ribwaff1.afficher_profil?p_id_req=60354349&p_cle=NOWLSUZQKM
【问题讨论】:
标签: python web-scraping beautifulsoup