在Python网站中显示所有搜索结果

2024-09-30 20:39:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python非常陌生,正在构建一个web scraper,它将刮取以下页面和其中的链接:https://www.nalpcanada.com/Page.cfm?PageID=33

问题是页面的默认值是显示前10个搜索结果,但是,我想刮所有150个搜索结果(当'全部'被选中,有150个链接)

我试过搞乱这个URL,但是不管选择了什么display results选项,这个URL都是静态的。我也试过在Chrome上查看开发人员工具的网络部分,但似乎不知道用什么来显示所有结果

以下是我目前的代码:

import bs4
import requests
import csv
import re

response = requests.get('https://www.nalpcanada.com/Page.cfm?PageID=33')
soup = bs4.BeautifulSoup(response.content, "html.parser")
urls = []

for a in soup.findAll('a', href=True, class_="employerProfileLink", text="Vancouver, British Columbia"):
    urls.append(a['href'])

pagesToCrawl = ['https://www.nalpcanada.com/' + url + '&QuestionTabID=47' for url in urls]

for pages in pagesToCrawl:
    html = requests.get(pages)
    soupObjs = bs4.BeautifulSoup(html.content, "html.parser")

    nameOfFirm = soupObjs.find('div', class_="ip-left").find('h2').next_element

    tbody = soupObjs.find('div', {"id":"collapse8"}).find('tbody')
    offers = tbody.find('td').next_sibling.next_sibling.next_element
    seeking = tbody.find('tr').next_sibling.next_sibling.find('td').next_sibling.next_sibling.next_element

    print('Firm name:', nameOfFirm)
    print('Offers:', offers)
    print('Seeking:', seeking)
    print('Hireback Rate:', int(offers) / int(seeking))

Tags: httpsimportcomforhtmlwwwfindrequests
1条回答
网友
1楼 · 发布于 2024-09-30 20:39:16

用此代码替换response调用似乎可行。原因是你没有正确地传递饼干

response = requests.get(
    'https://www.nalpcanada.com/Page.cfm',
    params={'PageID': 33},
    cookies={'DISPLAYNUM': '100000000'}
)

我遇到的另一个问题是,当某些链接(如YLaw Group)似乎没有“offers”和/或“seeking”时,这行代码会引发ValueError

print('Hireback Rate:', int(offers) / int(seeking))

我刚刚把这句话注释掉了,因为在这种情况下你得决定怎么办

相关问题 更多 >