【问题标题】:How To Scrape Each Line?如何刮掉每一行?
【发布时间】:2021-08-01 01:03:18
【问题描述】:

传统上,我使用beautifulsoup逐行解析。

在这种情况下这似乎不起作用,只是为我打印空白。

我想要每个职位发布的链接和标题

from bs4 import BeautifulSoup
import requests
import time


url='https://oysterpointrx.com/careers/'
r=requests.get(url)
time.sleep(4)
soup=BeautifulSoup(r.content,'html.parser')
content=soup.find_all('div',class_= 'opening')
for item in content:
    print(item.text)

【问题讨论】:

    标签: javascript python selenium web-scraping beautifulsoup


    【解决方案1】:

    这是你想要做的事情来抓取你所追求的内容:

    import requests
    from bs4 import BeautifulSoup
    
    link = 'https://boards.greenhouse.io/embed/job_board'
    params = {
        'for': 'oysterpointpharma',
        'b': 'https://oysterpointrx.com/careers/'
    }
    
    with requests.Session() as s:
        s.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
        res = s.get(link,params=params)
        soup = BeautifulSoup(res.text,"html.parser")
        for item in soup.select('.opening > a'):
            item_title = item.get_text(strip=True)
            item_link = item.get("href")
            print(item_title,item_link)
    

    【讨论】:

    • 漂亮,这工作!有没有办法获取这些标题的 href/url?
    【解决方案2】:

    你得到一个<Response [403]>,这意味着被禁止。您可以在声明变量后通过运行print(r) 来检查这一点。

    【讨论】:

    • 有没有办法绕过这个?
    • 现在意识到我应该对此发表评论,而不是作为答案发布。我不精通 Beautiful Soup,但我有预感您的请求出错了。正如您所见,SIM 的答案解决了这个问题。
    猜你喜欢
    • 2018-05-22
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-02
    • 2021-03-17
    相关资源
    最近更新 更多