簡體   English   中英

如何從標簽中提取文本?

[英]How to extract text from tag?

它給了我帶有html標簽的輸出,但我不需要 html 標簽。

獲取文本正在拋出 AttributeError:

'NoneType' object has no attribute 'get_text'

import requests
from bs4 import BeautifulSoup

url = requests.get("https://in.indeed.com/jobs?q=python%20developer&l=")

soup = BeautifulSoup(url.content,"html.parser")

parsed_file = soup.find(id = "resultsBody")


items = parsed_file.find_all(class_="slider_container")
for item in items:
    job_title = item.find(title='Python Developer').get_text()
    print(job_title)

get_text()僅在您的選擇有結果時才有效 - 只需檢查結果並處理它是否為true

for item in items:
    job_title = item.find(title='Python Developer').get_text() if item.find(title='Python Developer') else 'no result'
    print(job_title)

由於您只想打印標題為Python Developer的作業,因此您需要首先檢查是否存在具有此類標題的作業 -.find()不應返回None

只要把這張支票。

job_title = item.find(title='Python Developer')    
if job_title:
        print(job_title.get_text())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM